* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: #f5f5f5;
  color: #222;
  -webkit-text-size-adjust: 100%;
}

input, button, textarea {
  font-family: inherit;
  font-size: 16px; /* prevent iOS zoom on focus */
  padding: 12px 14px;
  border: 1px solid #ccc;
  border-radius: 8px;
  margin: 4px 0;
  width: 100%;
}
button {
  background: #2563eb;
  color: white;
  border: none;
  cursor: pointer;
  font-weight: 500;
  -webkit-tap-highlight-color: transparent;
}
button:hover { background: #1d4ed8; }
button:active { background: #1e40af; }
button:disabled { background: #999; cursor: not-allowed; }
.error { color: #dc2626; margin-top: 8px; min-height: 20px; font-size: 14px; }
h2, h3 { margin: 0 0 12px 0; }
a { color: #2563eb; }

/* ---------- ROOM ---------- */
.room-wrap {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 8px;
  max-width: 720px;
  margin: 0 auto;
}
.board-side { width: 100%; }
.info-side {
  width: 100%;
  background: white;
  padding: 12px;
  border-radius: 8px;
}

#board-wrap {
  display: grid;
  grid-template-columns: 22px 1fr;
  grid-template-rows: 1fr 22px;
  grid-template-areas:
    "ranks board"
    ".     files";
  width: 100%;
  max-width: 582px;
  margin: 0 auto;
  user-select: none;
}
#board {
  grid-area: board;
  width: 100%;
  aspect-ratio: 1 / 1;
}
.coords-ranks {
  grid-area: ranks;
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 600;
  color: #555;
  padding-right: 4px;
}
.coords-files {
  grid-area: files;
  display: flex;
  flex-direction: row;
  font-size: 13px;
  font-weight: 600;
  color: #555;
  padding-top: 2px;
}
.coords-ranks span, .coords-files span {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
@media (min-width: 720px) {
  #board-wrap {
    grid-template-columns: 24px 540px;
    grid-template-rows: 540px 24px;
    max-width: 564px;
  }
  .coords-ranks, .coords-files { font-size: 14px; }
}

.player-bar {
  padding: 10px 12px;
  margin: 4px 0;
  background: white;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  font-size: 15px;
}
.dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #aaa;
  display: inline-block;
  flex-shrink: 0;
}
.dot.online { background: #16a34a; }
.dot.your-turn { background: #f59e0b; box-shadow: 0 0 0 3px rgba(245,158,11,0.3); }

#status-line {
  padding: 12px 14px;
  margin-top: 8px;
  font-weight: 500;
  font-size: 15px;
  word-break: break-word;
  background: white;
  border-radius: 8px;
  transition: background-color .2s, color .2s;
}
#status-line.your-turn { background: #fef3c7; }
#status-line.check {
  background: #fee2e2;
  color: #991b1b;
  border: 2px solid #dc2626;
  font-weight: 600;
  animation: shake 0.5s ease-in-out 1;
}
#status-line .check-badge {
  display: inline-block;
  background: #dc2626;
  color: white;
  padding: 4px 10px;
  border-radius: 6px;
  font-weight: 700;
  font-size: 14px;
  margin-right: 8px;
  letter-spacing: 0.5px;
}
#status-line.finished-line {
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  padding: 16px;
}
#status-line.finished-line.win { background: #dcfce7; color: #166534; border: 2px solid #16a34a; }
#status-line.finished-line.lose { background: #fee2e2; color: #991b1b; border: 2px solid #dc2626; }
#status-line.finished-line.draw { background: #e0e7ff; color: #3730a3; border: 2px solid #4f46e5; }
#status-line .finish-icon-inline { font-size: 22px; margin-right: 6px; }

/* Stronger highlight on check square (chessground adds .check class to king square) */
cg-board square.check {
  background: radial-gradient(circle, rgba(220,38,38,0.9) 0%, rgba(220,38,38,0.5) 50%, transparent 80%) !important;
  box-shadow: inset 0 0 0 3px #dc2626;
}

/* Chessground coordinates — bigger, bolder, with shadow to stay readable */
.cg-wrap coords {
  font-size: 11px !important;
  font-weight: 700 !important;
  font-family: ui-monospace, "SF Mono", Menlo, monospace !important;
}
.cg-wrap coords coord {
  text-shadow:
    0 0 2px rgba(255,255,255,0.95),
    0 0 2px rgba(255,255,255,0.95),
    0 0 3px rgba(255,255,255,0.8) !important;
  padding: 2px 3px !important;
  letter-spacing: 0;
}
.cg-wrap coords.ranks { padding: 4px 0; }
.cg-wrap coords.files { padding: 0 4px; }
@media (min-width: 720px) {
  .cg-wrap coords { font-size: 13px !important; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* ----- Finish overlay (mat / draw) ----- */
.finish-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.25s ease-out;
  padding: 16px;
}
.finish-card {
  background: white;
  border-radius: 16px;
  padding: 32px 24px 24px;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  animation: popIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.finish-card.win { border-top: 6px solid #16a34a; }
.finish-card.lose { border-top: 6px solid #dc2626; }
.finish-card.draw { border-top: 6px solid #4f46e5; }
.finish-icon {
  font-size: 64px;
  line-height: 1;
  margin-bottom: 12px;
}
.finish-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 20px;
}
.finish-card.win .finish-title { color: #166534; }
.finish-card.lose .finish-title { color: #991b1b; }
.finish-card.draw .finish-title { color: #3730a3; }
.finish-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}
.finish-actions button {
  width: auto;
  padding: 10px 18px;
  font-size: 15px;
  margin: 0;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn {
  0% { opacity: 0; transform: scale(0.5); }
  100% { opacity: 1; transform: scale(1); }
}
#status-line code {
  display: block;
  font-size: 12px;
  color: #555;
  word-break: break-all;
  margin-top: 6px;
  padding: 6px;
  background: #f8f8f8;
  border-radius: 4px;
}
#status-line .share-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  flex-wrap: wrap;
}
#status-line .share-actions button {
  width: auto;
  padding: 8px 14px;
  font-size: 14px;
  margin: 0;
}
#status-line .share-actions .secondary {
  background: #6b7280;
}

#moves-list {
  max-height: 160px;
  overflow-y: auto;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 14px;
  padding: 8px;
  background: #fafafa;
  border-radius: 6px;
  margin-bottom: 12px;
}
.move-row { display: flex; gap: 6px; padding: 2px 0; }
.move-num { color: #888; min-width: 32px; }

#chat-messages {
  height: 200px;
  overflow-y: auto;
  padding: 8px;
  background: #fafafa;
  border-radius: 6px;
  font-size: 14px;
}
.chat-msg { padding: 4px 0; line-height: 1.4; }
.chat-msg .name { font-weight: 600; color: #2563eb; }
.chat-msg .time { font-size: 11px; color: #999; margin-left: 6px; }

#chat-form { display: flex; gap: 6px; margin-top: 8px; }
#chat-form input { flex: 1; margin: 0; }
#chat-form button { width: auto; padding: 10px 18px; margin: 0; }

.room-bottom-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.room-bottom-bar a { font-size: 14px; }
.room-bottom-bar .reset-btn {
  width: auto;
  padding: 8px 14px;
  font-size: 14px;
  margin: 0;
}
#room-link-info {
  font-size: 12px;
  color: #888;
  margin-left: auto;
}

/* ---------- LOBBY / LOGIN ---------- */
.login-view {
  max-width: 360px;
  margin: 60px auto 0;
  padding: 20px;
}
.lobby-view {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}
.lobby-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.lobby-header h2 { margin: 0; }
.lobby-user {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}
.lobby-user button {
  width: auto;
  padding: 6px 12px;
  font-size: 13px;
  margin: 0;
}
button.secondary { background: #6b7280; }
button.secondary:hover { background: #4b5563; }
.tg-link-hint {
  margin-top: 24px;
  font-size: 13px;
  color: #888;
  line-height: 1.5;
}
.link-tg-btn {
  width: auto;
  font-size: 13px;
  padding: 4px 10px;
  margin: 4px 0 0;
  display: inline-block;
}

.room-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px;
  background: white;
  margin: 8px 0;
  border-radius: 8px;
  gap: 8px;
}
.room-row a {
  text-decoration: none;
  font-weight: 500;
  font-size: 15px;
}
.room-row .room-id {
  font-size: 11px;
  color: #888;
  font-family: ui-monospace, monospace;
}

/* ---------- DESKTOP ---------- */
@media (min-width: 720px) {
  .room-wrap {
    flex-direction: row;
    gap: 24px;
    padding: 16px;
    align-items: flex-start;
    max-width: 1100px;
  }
  .board-side { flex: 0 0 auto; width: auto; }
  .info-side { flex: 1; min-width: 320px; max-width: 420px; }
  #board { width: 540px; max-width: none; }
  #moves-list { max-height: 220px; }
  #chat-messages { height: 260px; }
}
