:root {
  --bg: #f5f5f3;
  --bg-deep: #ecece7;
  --paper: rgba(255, 255, 255, 0.96);
  --paper-strong: #ffffff;
  --ink: #171717;
  --muted: #686868;
  --accent: #202020;
  --accent-strong: #000000;
  --accent-soft: rgba(32, 32, 32, 0.08);
  --line: rgba(23, 23, 23, 0.1);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
  --board-light: #f4dfb8;
  --board-dark: #a66d45;
  --board-pick: #efe58a;
  --board-move: rgba(42, 109, 64, 0.28);
  --board-capture: rgba(161, 41, 41, 0.3);
  --board-last: rgba(255, 208, 102, 0.32);
  --board-check: rgba(191, 37, 37, 0.4);
  --success: #2b6c49;
  --danger: #9f2b25;
  --font-title: "Georgia", "Times New Roman", "Source Han Serif SC", serif;
  --font-body: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-body);
  color: var(--ink);
  background: linear-gradient(180deg, #f7f7f5 0%, var(--bg) 100%);
}

body.modal-open {
  overflow: hidden;
}

button,
textarea,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1,
h2,
h3 {
  font-family: var(--font-title);
}

h1 {
  font-size: clamp(30px, 4vw, 42px);
  line-height: 0.92;
}

/* ─── Shell ─── */

.app-shell {
  width: min(1380px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0 40px;
}

/* ─── Header ─── */

.hero {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 24px;
}

.hero-brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.hero-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ─── Buttons ─── */

.primary-btn,
.secondary-btn,
.ghost-btn {
  min-height: 42px;
  padding: 0 16px;
  border-radius: 999px;
  font-weight: 600;
  transition: transform 120ms ease, opacity 120ms ease;
}

.primary-btn:hover,
.secondary-btn:hover,
.ghost-btn:hover {
  transform: translateY(-1px);
}

.primary-btn:active,
.secondary-btn:active,
.ghost-btn:active {
  transform: translateY(0);
  opacity: 0.8;
}

.primary-btn {
  background: var(--accent);
  color: #ffffff;
}

.secondary-btn {
  background: var(--accent-soft);
  color: var(--accent);
}

.ghost-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}

.small-btn {
  min-height: 30px;
  padding: 0 12px;
  font-size: 12px;
  border-radius: 10px;
  font-weight: 600;
}

.settings-btn {
  width: 38px;
  min-width: 38px;
  min-height: 38px;
  padding: 0;
  border-radius: 999px;
  display: grid;
  place-items: center;
}

.settings-btn svg {
  width: 17px;
  height: 17px;
  display: block;
}

/* ─── API status dot ─── */

.api-status-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  display: inline-block;
  transform: translateY(-1px);
}

.api-status-idle  { background: rgba(23, 23, 23, 0.22); }
.api-status-ok    { background: var(--success); box-shadow: 0 0 0 3px rgba(43, 108, 73, 0.15); }
.api-status-busy  { background: #9b7b2d; box-shadow: 0 0 0 3px rgba(155, 123, 45, 0.15); }
.api-status-error { background: var(--danger); box-shadow: 0 0 0 3px rgba(159, 43, 37, 0.15); }

/* ─── Layout ─── */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 760px) minmax(0, 640px);
  gap: 20px;
  align-items: stretch;
  justify-content: center;
}

.card {
  background: var(--paper);
  border: 1px solid rgba(23, 23, 23, 0.06);
  box-shadow: var(--shadow);
  border-radius: 20px;
}

/* ─── Board panel ─── */

.board-panel {
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 10px;
}

.board-wrap {
  display: grid;
  grid-template-columns: 20px minmax(0, 680px) 20px;
  gap: 8px;
  align-content: start;
}

.board-frame {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  align-items: stretch;
  gap: 8px;
}

.board {
  width: min(100%, 680px);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(23, 23, 23, 0.1);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  padding: 0;
  user-select: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: filter 80ms ease;
}

.square:hover {
  filter: brightness(1.07);
}

.square.light { background: var(--board-light); }
.square.dark  { background: var(--board-dark); }

.square.selected::after,
.square.last-move::after,
.square.check::after {
  content: "";
  position: absolute;
  inset: 0;
}

.square.selected::after  { background: var(--board-pick); }
.square.last-move::after { background: var(--board-last); }
.square.check::after     { background: var(--board-check); }

.square .piece {
  position: relative;
  z-index: 1;
  width: 82%;
  height: 82%;
  object-fit: contain;
  display: block;
  pointer-events: none;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.35));
}

.square .marker {
  position: absolute;
  inset: 50%;
  width: 18px;
  height: 18px;
  margin: -9px;
  border-radius: 999px;
  background: var(--board-move);
}

.square.capture .marker {
  width: calc(100% - 10px);
  height: calc(100% - 10px);
  margin: calc(-50% + 5px);
  background: transparent;
  border: 3px solid var(--board-capture);
}

/* ─── Coordinate labels ─── */

.file-labels,
.rank-labels {
  display: grid;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.file-labels {
  grid-template-columns: repeat(8, 1fr);
  grid-column: 2 / 3;
  width: 100%;
  justify-items: center;
  text-align: center;
}

.rank-labels {
  grid-template-rows: repeat(8, 1fr);
  align-items: center;
}

.rank-labels.left  { justify-items: end; }
.rank-labels.right { justify-items: start; }

/* ─── Game status bar ─── */

.game-status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--bg-deep);
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  transition: background 200ms ease, color 200ms ease;
}

.game-status::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.55;
  flex: 0 0 auto;
}

.game-status.is-check {
  background: rgba(159, 43, 37, 0.09);
  color: var(--danger);
}

.game-status.is-gameover {
  background: rgba(23, 23, 23, 0.05);
  color: var(--ink);
  font-weight: 700;
}

/* ─── Coach panel (container only, no card) ─── */

.coach-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ─── Chat panel (the actual card) ─── */

.chat-panel {
  flex: 1 1 auto;
  min-height: 0;
  border-radius: 20px;
  background: var(--paper);
  border: 1px solid rgba(23, 23, 23, 0.06);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow: hidden;
}

/* ─── Coach notification ─── */

.coach-notification {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  padding: 7px 12px;
  border-radius: 10px;
  background: rgba(244, 230, 190, 0.6);
  border: 1px solid rgba(180, 140, 60, 0.18);
  min-height: 34px;
  overflow: hidden;
}

.coach-notification.is-hidden,
.capture-strip.is-hidden {
  display: none;
}

.coach-notification-label {
  font-size: 10px;
  opacity: 0.5;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
  flex: 0 0 auto;
}

.coach-notification-title {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
}

.coach-notification-copy {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 0 1 auto;
  min-width: 0;
  display: none;
}

/* ─── Capture strip ─── */

.capture-strip {
  min-height: 32px;
  margin-bottom: 10px;
  padding: 5px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.55);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
}

.capture-group {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 2px;
  overflow: hidden;
  white-space: nowrap;
}

.capture-group-white { justify-content: flex-end; }

.capture-divider {
  width: 1px;
  height: 14px;
  background: var(--line);
}

.capture-piece {
  width: 20px;
  height: 20px;
  object-fit: contain;
  display: block;
  flex: 0 0 auto;
}

/* ─── Chat messages ─── */

.chat-messages {
  overflow: auto;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  gap: 10px;
  padding: 4px 0;
  min-height: 0;
  align-items: stretch;
  justify-content: flex-end;
}

.chat-message { display: block; }

.chat-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.chat-stack {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  max-width: min(84%, 520px);
}

.chat-avatar {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  flex: 0 0 auto;
  background: var(--bg-deep);
  color: var(--ink);
}

.chat-role {
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.02em;
  padding: 0 2px;
}

.chat-bubble {
  display: inline-block;
  padding: 9px 13px;
  border-radius: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  width: fit-content;
  max-width: 100%;
  word-break: normal;
  overflow-wrap: break-word;
  font-size: 14px;
}

.chat-message.user {
  display: flex;
  justify-content: flex-end;
}

.chat-message.user .chat-row { flex-direction: row-reverse; }
.chat-message.user .chat-stack { align-items: flex-end; }
.chat-message.user .chat-role { text-align: right; }

.chat-message.user .chat-avatar {
  background: var(--accent);
  color: #ffffff;
}

.chat-message.user .chat-bubble {
  background: var(--accent);
  color: #ffffff;
}

.chat-message.assistant .chat-bubble {
  background: var(--bg-deep);
  border: 1px solid rgba(23, 23, 23, 0.04);
  color: var(--ink);
}

/* ─── Chat form ─── */

.chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  margin-top: auto;
  padding-top: 14px;
  align-items: center;
  border-top: 1px solid var(--line);
}

.chat-form input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg-deep);
  color: var(--ink);
  border-radius: 12px;
  padding: 0 14px;
  min-height: 42px;
  outline: none;
  transition: border-color 120ms ease, background 120ms ease;
}

.chat-form input:focus {
  border-color: rgba(23, 23, 23, 0.26);
  background: var(--paper-strong);
}

.chat-form input::placeholder {
  color: var(--muted);
  opacity: 0.55;
}

.chat-send-btn {
  width: 42px;
  min-height: 42px;
  min-width: 42px;
  padding: 0;
  border-radius: 12px;
  display: grid;
  place-items: center;
}

.chat-send-btn svg {
  width: 16px;
  height: 16px;
}

/* ─── Toast ─── */

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(23, 23, 23, 0.92);
  color: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 20;
  font-size: 14px;
}

/* ─── Settings modal (centered) ─── */

.settings-modal[hidden] {
  display: none;
}

.settings-modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.settings-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 15, 15, 0.2);
  backdrop-filter: blur(8px);
}

.settings-dialog {
  position: relative;
  width: min(400px, 100%);
  padding: 20px;
  border-radius: 20px;
  border: 1px solid rgba(23, 23, 23, 0.08);
  background: var(--paper-strong);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.14);
  display: grid;
  gap: 16px;
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.settings-head h2 {
  font-size: 20px;
  line-height: 1;
}

.settings-close-btn {
  width: 32px;
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 18px;
}

.settings-grid {
  display: grid;
  gap: 12px;
}

.api-field {
  min-width: 0;
  display: grid;
  gap: 5px;
}

.api-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.api-field input,
.api-field select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-deep);
  color: var(--ink);
  padding: 0 12px;
  outline: none;
  transition: border-color 120ms ease, background 120ms ease;
}

.api-field input:focus,
.api-field select:focus {
  border-color: rgba(23, 23, 23, 0.26);
  background: var(--paper-strong);
}

.settings-actions {
  display: flex;
  justify-content: flex-end;
}

.settings-save-btn {
  min-height: 38px;
  padding: 0 20px;
  border-radius: 12px;
  font-size: 14px;
}

/* ─── Responsive ─── */

/* Tablet landscape → single column */
@media (max-width: 1080px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .coach-panel {
    min-height: 0;
  }

  .chat-messages {
    flex: none;
    height: 340px;
  }
}

/* Tablet portrait / large phone */
@media (max-width: 760px) {
  .app-shell {
    width: calc(100vw - 20px);
    padding-top: 16px;
    padding-bottom: 32px;
  }

  .layout {
    gap: 14px;
  }

  .board-panel {
    padding: 14px;
    border-radius: 18px;
  }

  .chat-panel {
    padding: 16px;
    border-radius: 18px;
  }

  .board-frame {
    gap: 6px;
  }

  .square {
    font-size: clamp(22px, 8vw, 36px);
  }

  .coach-panel {
    min-height: 0;
  }

  .chat-messages {
    height: 300px;
  }

  .small-btn {
    min-height: 36px;
  }

  .settings-modal {
    align-items: flex-end;
    padding: 0;
  }

  .settings-dialog {
    position: relative;
    width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: 80vh;
    overflow-y: auto;
    padding-bottom: max(20px, env(safe-area-inset-bottom));
  }

  .game-status {
    font-size: 12px;
    padding: 7px 12px;
    margin-top: 10px;
  }
}

/* Small phone: fix board overflow (minmax(280px) overflows at <390px) */
@media (max-width: 480px) {
  .board-wrap {
    grid-template-columns: 14px minmax(0, 680px) 14px;
    gap: 5px;
  }

  .board-frame {
    gap: 5px;
  }

  .file-labels,
  .rank-labels {
    font-size: 10px;
  }

  .rank-labels.right {
    display: none;
  }

  .square {
    font-size: clamp(20px, 9.5vw, 34px);
  }

  .coach-panel {
    min-height: 0;
  }

  .chat-messages {
    height: 260px;
  }

  .chat-panel {
    padding: 14px;
    border-radius: 16px;
  }

  .chat-bubble {
    font-size: 13px;
  }
}

/* Very small phone — iPhone SE (375px) */
@media (max-width: 400px) {
  .app-shell {
    width: calc(100vw - 14px);
    padding-top: 14px;
  }

  .layout {
    gap: 12px;
  }

  .board-panel {
    padding: 12px;
    border-radius: 16px;
  }

  h1 {
    font-size: 26px;
  }

  .hero {
    margin-bottom: 16px;
  }

  .coach-panel {
    min-height: 0;
  }

  .chat-messages {
    height: 240px;
  }
}

/* Safe area insets — iPhone notch / home indicator */
@supports (padding: env(safe-area-inset-bottom)) {
  .app-shell {
    padding-bottom: calc(32px + env(safe-area-inset-bottom));
  }

  .app-shell {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
}
