:root {
  color-scheme: light;
  --bg: #f2f2f7; /* iOS systemGroupedBackground */
  --panel: #ffffff;
  --ink: #1c1c1e; /* iOS label */
  --muted: rgba(60, 60, 67, 0.6); /* iOS secondaryLabel */
  --line: rgba(60, 60, 67, 0.29); /* iOS separator */
  --fill: rgba(120, 120, 128, 0.12); /* iOS tertiarySystemFill */
  --accent: #007aff; /* iOS systemBlue */
  --accent-strong: #0062cc;
  --accent-soft: rgba(0, 122, 255, 0.12);
  --success-soft: rgba(52, 199, 89, 0.16); /* iOS systemGreen tint */
  --danger: #ff3b30; /* iOS systemRed */
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  --radius: 14px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue",
    "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button,
select {
  font: inherit;
  touch-action: manipulation;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 48px;
}

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

.hero h1 {
  margin: 0 0 10px;
  font-size: clamp(2rem, 4vw, 3.5rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.hero p {
  margin: 0;
  max-width: 760px;
  color: var(--muted);
  font-size: 1.08rem;
  line-height: 1.6;
}

.setup-panel {
  flex: 0 0 auto;
  display: flex;
  align-items: end;
  gap: 12px;
  padding: 14px;
  border: 0;
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.setup-panel label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
}

select {
  min-width: 150px;
  border: 0;
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--fill);
  color: var(--ink);
  font-weight: 500;
}

.primary,
.secondary {
  border: 0;
  border-radius: 12px;
  padding: 11px 18px;
  font-weight: 600;
}

.primary {
  background: var(--accent);
  color: white;
}

.primary:hover:not(:disabled) {
  background: var(--accent-strong);
}

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

.scoreboard {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.score-card {
  border: 0;
  border-radius: var(--radius);
  background: var(--panel);
  padding: 16px;
  box-shadow: var(--shadow);
}

.score-card.active {
  outline: 2px solid rgba(0, 122, 255, 0.4);
  background: var(--accent-soft);
}

.score-name {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 12px;
}

.score-row {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  margin-bottom: 6px;
}

.score-row strong {
  color: var(--ink);
}

.books {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  min-height: 27px;
}

.book {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--success-soft);
  border: 0;
  color: #248a3d;
  font-weight: 600;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 18px;
}

.table-panel,
.log-panel {
  border: 0;
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.table-panel {
  padding: 20px;
}

.table-top,
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.table-top h2,
.hand-panel h2,
.log-panel h2 {
  margin: 0 0 4px;
  letter-spacing: -0.03em;
}

.muted {
  color: var(--muted);
}

.pond {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: 0;
  border-radius: 12px;
  background: var(--fill);
  color: var(--muted);
}

.pond-card {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 52px;
  border-radius: 9px;
  background: linear-gradient(135deg, #0a84ff, #5ac8fa);
  color: white;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0, 122, 255, 0.3);
}

.opponents {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 12px;
  margin: 18px 0;
}

.opponent-card {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: white;
  padding: 14px;
}

.opponent-card:hover:not(:disabled),
.opponent-card.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.opponent-name,
.opponent-meta,
.target-label,
.mini-hand {
  display: block;
}

.opponent-name {
  font-weight: 600;
  margin-bottom: 4px;
}

.opponent-meta,
.target-label {
  color: var(--muted);
  font-size: 0.9rem;
}

.target-label {
  margin-top: 10px;
  font-weight: 600;
  color: var(--accent);
}

.mini-hand {
  min-height: 36px;
  margin-top: 10px;
}

.controls {
  border: 0;
  border-radius: var(--radius);
  padding: 16px;
  background: var(--bg);
  margin-bottom: 18px;
}

.turn-status {
  margin: 0 0 5px;
  font-weight: 600;
}

.action-preview {
  margin: 0;
  color: var(--muted);
}

.hand-panel {
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.rank-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.rank-choice {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: white;
  padding: 13px;
  text-align: left;
}

.rank-choice:hover:not(:disabled),
.rank-choice.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.rank-title,
.rank-count,
.rank-cards {
  display: block;
}

.rank-title {
  font-size: 1rem;
  font-weight: 600;
}

.rank-count {
  margin: 2px 0 9px;
  color: var(--muted);
  font-size: 0.88rem;
}

.rank-cards,
.mini-hand {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
}

.card {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  width: 36px;
  height: 50px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: white;
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.card > span:first-child {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 0.62rem;
  line-height: 1;
}

.card > span:last-child {
  font-size: 1.15rem;
  line-height: 1;
  margin-top: 7px;
}

.card.red {
  color: var(--danger);
}

.card-back {
  min-width: 24px;
  height: 32px;
  border-color: transparent;
  background: linear-gradient(135deg, #0a84ff, #5ac8fa);
  color: white;
  justify-content: center;
  font-size: 0.9rem;
}

.empty-hand {
  border: 1px dashed var(--line);
  border-radius: 16px;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.log-panel {
  padding: 18px;
  align-self: start;
  position: sticky;
  top: 18px;
}

.log-list {
  margin: 14px 0 0;
  padding-left: 20px;
  color: var(--muted);
  line-height: 1.5;
}

.log-list li {
  margin-bottom: 9px;
}

.error {
  margin-top: 10px;
  border: 0;
  border-radius: 12px;
  background: rgba(255, 59, 48, 0.12);
  color: #d70015;
  padding: 10px;
  font-weight: 600;
}

@media (max-width: 900px) {
  .hero,
  .table-top,
  .controls {
    align-items: stretch;
    flex-direction: column;
  }

  .setup-panel {
    width: 100%;
    align-items: stretch;
    flex-direction: column;
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .log-panel {
    position: static;
  }
}

/* ---------- Mobile (phones / embedded webviews): dense, one-screen layout ---------- */
@media (max-width: 640px) {
  body {
    min-height: 100dvh;
  }

  .app-shell {
    width: 100%;
    padding: calc(6px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right))
      calc(88px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
  }

  /* One-row header: title + player count + new game */
  .hero {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
  }

  .hero h1 {
    margin: 0;
    font-size: 1.25rem;
    letter-spacing: -0.03em;
    white-space: nowrap;
  }

  .hero p {
    display: none;
  }

  .setup-panel {
    flex: 1 1 auto;
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
    padding: 0;
    gap: 6px;
    border: 0;
    background: transparent;
    box-shadow: none;
  }

  .setup-panel label {
    flex: 0 1 128px;
    font-size: 0; /* visually hide the "Players" caption, keep it for a11y */
    gap: 0;
  }

  select {
    width: 100%;
    min-width: 0;
    min-height: 38px;
    padding: 7px 8px;
    font-size: 16px; /* prevents iOS auto-zoom on focus */
  }

  .primary,
  .secondary {
    min-height: 38px;
    font-size: 14px;
    padding: 7px 12px;
    white-space: nowrap;
  }

  /* Slim scoreboard rows */
  .scoreboard {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
    margin-bottom: 6px;
  }

  .score-card {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 8px;
    padding: 5px 9px;
    border-radius: 11px;
    box-shadow: none;
  }

  .score-name {
    font-size: 0.85rem;
    margin: 0;
  }

  .score-row {
    margin: 0;
    gap: 3px;
    font-size: 0.78rem;
  }

  .books {
    display: none; /* books count already shown in the row */
  }

  .game-layout {
    gap: 8px;
  }

  .table-panel {
    padding: 10px;
  }

  /* Compact table top with inline pond counter */
  .table-top {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }

  .table-top h2 {
    font-size: 1rem;
    margin: 0;
  }

  .table-top .muted {
    display: none;
  }

  .pond {
    padding: 4px 10px;
    gap: 8px;
    border-radius: 12px;
    font-size: 0.85rem;
    white-space: nowrap;
  }

  .pond-card {
    width: 22px;
    height: 30px;
    border-radius: 6px;
    font-size: 0.8rem;
  }

  /* Opponents as a horizontal row of tappable chips */
  .opponents {
    display: flex;
    overflow-x: auto;
    gap: 6px;
    margin: 8px 0;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
  }

  .opponent-card {
    flex: 0 0 auto;
    width: auto;
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 7px 13px;
    min-height: 44px;
    border-radius: 999px;
  }

  .opponent-name {
    margin: 0;
    font-size: 0.9rem;
  }

  .opponent-meta {
    font-size: 0.78rem;
  }

  .target-label {
    display: none; /* selection is shown by the highlighted chip */
  }

  .mini-hand {
    display: flex;
    gap: 2px;
    margin: 0;
    min-height: 0;
  }

  .opponent-card .card-back {
    min-width: 14px;
    width: 14px;
    height: 20px;
    border-radius: 4px;
    font-size: 0.6rem;
  }

  /* Fixed bottom action bar so "Ask" is always thumb-reachable */
  .controls {
    position: fixed;
    left: calc(10px + env(safe-area-inset-left));
    right: calc(10px + env(safe-area-inset-right));
    bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 50;
    margin-bottom: 0;
    padding: 10px 12px;
    background: rgba(249, 249, 249, 0.94);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    backdrop-filter: blur(20px) saturate(180%);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
    flex-direction: row;
    align-items: center;
  }

  .controls > div {
    min-width: 0;
  }

  .turn-status {
    margin: 0 0 2px;
    font-size: 0.85rem;
  }

  .action-preview {
    font-size: 0.8rem;
  }

  #ask-button {
    flex: 0 0 auto;
    min-width: 84px;
    min-height: 46px;
  }

  /* Hand: compact rank chips, three per row */
  .hand-panel {
    padding-top: 8px;
  }

  .hand-panel h2 {
    font-size: 1rem;
  }

  .rank-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  .rank-choice {
    padding: 7px 5px 9px;
    border-radius: 11px;
    min-height: 46px;
    text-align: center;
  }

  .rank-title {
    font-size: 0.9rem;
  }

  .rank-count {
    margin: 1px 0 0;
    font-size: 0.75rem;
  }

  .rank-cards {
    display: flex;
    justify-content: center;
    gap: 3px;
    margin-top: 4px;
  }

  .rank-choice .card {
    min-width: 24px;
    width: 24px;
    height: 34px;
    padding: 1px;
  }

  .rank-choice .card > span:first-child {
    top: 1px;
    left: 3px;
    font-size: 0.5rem;
  }

  .rank-choice .card > span:last-child {
    font-size: 0.85rem;
    margin-top: 6px;
  }

  .empty-hand {
    padding: 14px;
    font-size: 0.9rem;
  }

  /* Log: compact, latest two entries only */
  .log-panel {
    padding: 10px 12px;
  }

  .log-panel h2 {
    font-size: 0.95rem;
  }

  .log-list {
    margin: 6px 0 0;
    padding-left: 18px;
    font-size: 0.85rem;
    line-height: 1.4;
  }

  .log-list li {
    margin-bottom: 4px;
  }

  .log-list li:nth-child(n + 3) {
    display: none;
  }

  .log-panel h2 {
    display: inline;
    margin-right: 6px;
  }

  .log-list {
    display: inline;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .log-list li {
    display: inline;
    margin: 0;
  }

  .log-list li:nth-child(n + 2) {
    display: none;
  }

  /* Hover states don't exist on touch — avoid stuck highlights */
  .opponent-card:hover:not(:disabled):not(.selected),
  .rank-choice:hover:not(:disabled):not(.selected) {
    border-color: var(--line);
    background: white;
  }
}

/* ---------- Subtle mobile polish (original palette) ---------- */
@media (max-width: 640px) {
  .rank-choice,
  .opponent-card {
    box-shadow: 0 2px 6px rgba(23, 32, 51, 0.06);
  }

  .rank-choice.selected,
  .opponent-card.selected {
    box-shadow: 0 3px 10px rgba(20, 89, 217, 0.18);
  }

  .pond {
    background: var(--fill);
  }

  .controls {
    border-top: 0.5px solid var(--line);
  }
}

/* ---------- Motion & header controls (same design, gentle animation) ---------- */
.lang-select {
  min-width: 0;
  width: auto;
}

.icon-button {
  padding: 10px 12px;
  line-height: 1;
}

.primary,
.secondary,
.rank-choice,
.opponent-card {
  transition: transform 0.12s ease, background-color 0.15s ease, border-color 0.15s ease,
    box-shadow 0.15s ease;
}

.primary:active:not(:disabled),
.secondary:active:not(:disabled),
.rank-choice:active:not(:disabled),
.opponent-card:active:not(:disabled) {
  transform: scale(0.97);
}

@keyframes deal-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.94);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.rank-grid.deal .rank-choice {
  animation: deal-in 0.35s ease both;
}

.rank-grid.deal .rank-choice:nth-child(2) { animation-delay: 0.05s; }
.rank-grid.deal .rank-choice:nth-child(3) { animation-delay: 0.1s; }
.rank-grid.deal .rank-choice:nth-child(4) { animation-delay: 0.15s; }
.rank-grid.deal .rank-choice:nth-child(5) { animation-delay: 0.2s; }
.rank-grid.deal .rank-choice:nth-child(6) { animation-delay: 0.25s; }
.rank-grid.deal .rank-choice:nth-child(7) { animation-delay: 0.3s; }
.rank-grid.deal .rank-choice:nth-child(n + 8) { animation-delay: 0.35s; }

@keyframes thinking-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

.turn-status.thinking {
  animation: thinking-pulse 1.3s ease-in-out infinite;
}

@media (max-width: 640px) {
  .lang-select {
    flex: 0 0 auto;
    width: auto;
    min-height: 38px;
    font-size: 14px;
  }

  .icon-button {
    min-width: 38px;
    padding: 7px 9px;
  }

  .setup-panel {
    flex-wrap: nowrap;
  }

  .setup-panel label {
    flex: 1 1 96px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
