:root {
  --bg-a: #f2f7ff;
  --bg-b: #d8f2e7;
  --bg-c: #fef4da;
  --surface: rgba(255, 255, 255, 0.86);
  --surface-strong: #ffffff;
  --text: #14202c;
  --muted: #5f6d78;
  --line: #d2dde5;
  --primary: #0d9488;
  --primary-hover: #0f766e;
  --accent: #f59e0b;
  --success: #0f766e;
  --warning: #b45309;
  --error: #b42318;
  --bull: #0f766e;
  --cow: #b45309;
  --miss: #8896a1;
  --shadow: 0 28px 50px rgba(17, 45, 60, 0.16);
  --radius-lg: 24px;
  --radius-md: 14px;
  --progress-hue: 160;
}

body[data-theme="dark"] {
  --bg-a: #0a1116;
  --bg-b: #10202b;
  --bg-c: #1e1a12;
  --surface: rgba(13, 22, 28, 0.88);
  --surface-strong: #0f1a22;
  --text: #e8f2f8;
  --muted: #afbfca;
  --line: #2a3a46;
  --primary: #2ec4b6;
  --primary-hover: #23a99d;
  --accent: #f7b84b;
  --success: #53d8c8;
  --warning: #f5b14d;
  --error: #fb8a81;
  --bull: #5be3d0;
  --cow: #ffc46f;
  --miss: #5e6f7a;
  --shadow: 0 34px 60px rgba(2, 7, 10, 0.6);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

body {
  font-family: "Space Grotesk", sans-serif;
  color: var(--text);
  min-height: 100vh;
  background:
    radial-gradient(80% 120% at 15% 0%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 56%),
    radial-gradient(70% 100% at 95% 0%, color-mix(in srgb, var(--primary) 18%, transparent), transparent 60%),
    linear-gradient(130deg, var(--bg-a), var(--bg-b), var(--bg-c));
  transition: background 400ms ease, color 300ms ease;
}

/* ── Confetti canvas ── */
#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
}

.app {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 22px;
}

.card {
  width: min(780px, 100%);
  background: var(--surface);
  border: 1px solid color-mix(in srgb, var(--line) 65%, white 35%);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
  padding: 24px;
  position: relative;
  overflow: hidden;
  animation: rise 380ms ease-out;
  transition: background 400ms ease, border-color 300ms ease, box-shadow 400ms ease;
}

.card::after {
  content: "";
  position: absolute;
  width: 320px;
  aspect-ratio: 1;
  right: -140px;
  bottom: -140px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 35%, color-mix(in srgb, var(--primary) 26%, transparent), transparent 68%);
  pointer-events: none;
}

/* ── Top bar ── */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.eyebrow {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--primary);
  transition: color 300ms ease;
}

/* ── Live timer ── */
.live-timer {
  font-family: "DM Mono", monospace;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface-strong) 84%, transparent);
  transition: color 300ms ease, background 300ms ease, border-color 300ms ease;
}

.live-timer.running {
  color: var(--primary);
}

.icon-btn {
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface-strong) 84%, transparent);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 12px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 700;
  transition: transform 130ms ease, border-color 130ms ease, background-color 300ms ease, color 300ms ease;
}

.icon-btn:hover {
  transform: translateY(-1px);
  border-color: color-mix(in srgb, var(--primary) 40%, var(--line));
}

.btn-icon {
  width: 16px;
  height: 16px;
  display: inline-grid;
  place-items: center;
}

.btn-icon svg {
  width: 100%;
  height: 100%;
}

/* ── Title ── */
h1 {
  margin: 8px 0 10px;
  font-size: clamp(30px, 4vw, 46px);
  line-height: 1.08;
  transition: color 300ms ease;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  transition: color 300ms ease;
}

.title-icons {
  display: inline-flex;
  gap: 8px;
  margin-left: 8px;
  transform: translateY(-2px);
}

.icon-badge {
  width: 28px;
  height: 28px;
  border-radius: 10px;
  border: 1px solid var(--line);
  display: inline-grid;
  place-items: center;
  background: color-mix(in srgb, var(--surface-strong) 88%, transparent);
  transition: background 300ms ease, border-color 300ms ease;
}

.icon-badge svg {
  width: 17px;
  height: 17px;
}

/* ── Stats grid ── */
.stats-grid {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.stat-chip {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 11px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface-strong) 88%, transparent);
  transition: background 300ms ease, border-color 300ms ease;
}

.chip-icon {
  width: 30px;
  height: 30px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--primary) 30%, var(--line));
  color: var(--primary);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  transition: color 300ms ease, border-color 300ms ease;
}

.chip-icon svg {
  width: 18px;
  height: 18px;
}

.chip-title,
.chip-value {
  margin: 0;
}

.chip-title {
  color: var(--muted);
  font-size: 12px;
  transition: color 300ms ease;
}

.chip-value {
  font-size: 14px;
  font-weight: 700;
  transition: color 300ms ease;
}

/* ── Difficulty ── */
.difficulty-wrap {
  margin-top: 14px;
}

.difficulty-wrap h2 {
  margin: 0 0 9px;
  font-size: 16px;
  transition: color 300ms ease;
}

.difficulty-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.difficulty-btn {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 13px;
  background: color-mix(in srgb, var(--surface-strong) 88%, transparent);
  color: color-mix(in srgb, var(--text) 88%, var(--muted));
  transition: background 200ms ease, border-color 200ms ease, color 200ms ease, transform 130ms ease;
}

.difficulty-btn.active {
  background: color-mix(in srgb, var(--primary) 16%, var(--surface-strong));
  border-color: color-mix(in srgb, var(--primary) 45%, var(--line));
  color: var(--text);
}

/* ── Collapsible rules ── */
.rules {
  margin-top: 16px;
  border: 1px dashed color-mix(in srgb, var(--line) 78%, var(--primary) 22%);
  border-radius: 16px;
  padding: 0;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  overflow: hidden;
  transition: background 300ms ease, border-color 300ms ease;
}

.rules-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 14px;
  background: none;
  border: 0;
  border-radius: 0;
  cursor: pointer;
  color: var(--text);
  font: inherit;
  font-weight: 700;
  transition: color 300ms ease;
}

.rules-toggle:hover,
.rules-toggle:hover:not(:disabled) {
  transform: none;
  background: color-mix(in srgb, var(--accent) 6%, transparent);
}

.rules-toggle:focus-visible {
  outline-offset: -2px;
  border-radius: 16px;
}

.rules-toggle h2 {
  margin: 0;
  font-size: 18px;
  pointer-events: none;
}

.rules-chevron {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  transition: transform 250ms ease;
}

.rules-chevron svg {
  width: 100%;
  height: 100%;
}

.rules.collapsed .rules-chevron {
  transform: rotate(0deg);
}

.rules:not(.collapsed) .rules-chevron {
  transform: rotate(180deg);
}

.rules-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 350ms ease-out, opacity 250ms ease;
  opacity: 0;
}

.rules:not(.collapsed) .rules-content {
  max-height: 400px;
  opacity: 1;
}

.rules-list {
  list-style: none;
  margin: 0;
  padding: 4px 14px 14px;
  display: grid;
  gap: 8px;
}

.rules-list li {
  display: flex;
  gap: 8px;
  color: color-mix(in srgb, var(--text) 86%, var(--muted));
  align-items: flex-start;
  line-height: 1.35;
  font-size: 14px;
}

.rule-text {
  display: block;
  min-width: 0;
  flex: 1;
}

.rule-icon {
  margin-top: 1px;
  width: 19px;
  height: 19px;
  border-radius: 6px;
  color: var(--primary);
  display: inline-grid;
  place-items: center;
  border: 1px solid color-mix(in srgb, var(--primary) 34%, var(--line));
  flex-shrink: 0;
  transition: color 300ms ease, border-color 300ms ease;
}

.rule-icon svg {
  width: 13px;
  height: 13px;
}

/* ── Guess form ── */
.guess-form {
  margin-top: 16px;
}

.guess-form label {
  display: inline-block;
  margin-bottom: 8px;
  font-weight: 700;
  transition: color 300ms ease;
}

.input-row {
  display: flex;
  gap: 10px;
  align-items: stretch;
}

.input-row input {
  flex: 1;
  min-width: 0;
}

input,
button {
  font: inherit;
}

input {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  background: color-mix(in srgb, var(--surface-strong) 90%, transparent);
  color: var(--text);
  font-family: "DM Mono", monospace;
  font-size: 18px;
  letter-spacing: 0.13em;
  transition: background 300ms ease, border-color 300ms ease, color 300ms ease;
}

input::placeholder {
  color: color-mix(in srgb, var(--muted) 70%, transparent);
}

input:focus-visible,
button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--primary) 35%, transparent);
  outline-offset: 1px;
}

button {
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 130ms ease, background-color 200ms ease, opacity 130ms ease, color 300ms ease, border-color 300ms ease;
}

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

button:hover:not(:disabled) {
  transform: translateY(-1px);
}

button[type="submit"] {
  background: var(--primary);
  color: #fff;
}

button[type="submit"]:hover:not(:disabled) {
  background: var(--primary-hover);
}

/* ── Numpad ── */
.numpad {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.numpad-key {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 0;
  font-family: "DM Mono", monospace;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  background: color-mix(in srgb, var(--surface-strong) 88%, transparent);
  transition: transform 80ms ease, background 150ms ease, border-color 150ms ease, opacity 200ms ease, color 300ms ease;
}

.numpad-key.action {
  font-family: "Space Grotesk", sans-serif;
  font-size: 12px;
  letter-spacing: 0.05em;
}

.numpad-key.action {
  grid-column: span 1;
}

.numpad-key.action.wide {
  grid-column: span 2;
}

.numpad-key:hover:not(:disabled) {
  border-color: color-mix(in srgb, var(--primary) 40%, var(--line));
  background: color-mix(in srgb, var(--primary) 14%, var(--surface-strong));
}

.numpad-key:active:not(:disabled) {
  transform: scale(0.93);
  background: color-mix(in srgb, var(--primary) 22%, var(--surface-strong));
}

.numpad-key.used {
  opacity: 0.35;
  pointer-events: none;
}

/* ── Inline new game button ── */
.inline-new-game {
  display: none;
}

.inline-new-game.visible {
  display: block;
}

.secondary {
  background: color-mix(in srgb, var(--surface-strong) 83%, var(--line));
  color: var(--text);
  border: 1px solid var(--line);
  transition: transform 130ms ease, background 200ms ease, border-color 300ms ease, color 300ms ease;
}

.secondary:hover:not(:disabled) {
  background: color-mix(in srgb, var(--surface-strong) 72%, var(--line));
}

/* ── Hint & consistency (hidden for competitive play) ── */
.latest-hint {
  margin: 8px 0 0;
  color: color-mix(in srgb, var(--text) 84%, var(--primary));
  font-size: 13px;
  font-weight: 700;
  display: none;
}

.consistency-text {
  margin: 4px 0 0;
  font-size: 12px;
  color: color-mix(in srgb, var(--text) 78%, var(--muted));
  font-weight: 600;
  display: none;
}

.consistency-text.warning {
  color: var(--warning);
}

.consistency-text.error {
  color: var(--error);
}

/* ── Attempts & progress ── */
.attempts-panel {
  margin: 14px 0 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.attempt-counter {
  margin: 0;
  color: color-mix(in srgb, var(--text) 78%, var(--muted));
  font-size: 14px;
  font-weight: 600;
  transition: color 300ms ease;
}

.attempts-left {
  margin: 0;
  font-size: 13px;
  color: var(--warning);
  font-weight: 700;
}

.progress-wrap {
  margin-top: 8px;
  border-radius: 999px;
  height: 9px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface-strong) 82%, transparent);
  overflow: hidden;
  transition: border-color 300ms ease, background 300ms ease;
}

.progress-bar {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--primary);
  transition: width 220ms ease, background 400ms ease;
}

.progress-bar.low {
  background: var(--success);
}

.progress-bar.mid {
  background: var(--accent);
}

.progress-bar.high {
  background: var(--error);
}

/* ── Message ── */
.message {
  min-height: 1.5em;
  margin: 10px 0 0;
  font-weight: 700;
  transition: color 300ms ease;
}

.message.error {
  color: var(--error);
}

.message.success {
  color: var(--success);
}

.message.warning {
  color: var(--warning);
}

.message.info {
  color: color-mix(in srgb, var(--text) 82%, var(--primary));
}

/* ── Postgame ── */
.postgame-panel {
  margin-top: 10px;
  border: 1px solid color-mix(in srgb, var(--line) 75%, var(--primary) 25%);
  border-radius: 14px;
  padding: 12px;
  background: color-mix(in srgb, var(--surface-strong) 86%, transparent);
  animation: pop 250ms ease-out;
  transition: background 300ms ease, border-color 300ms ease;
}

.postgame-panel h2 {
  margin: 0 0 8px;
  font-size: 16px;
}

.postgame-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.postgame-grid p {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.postgame-grid span {
  color: var(--text);
  font-weight: 700;
  margin-left: 6px;
}

/* ── Actions ── */
.actions {
  margin-top: 12px;
  display: flex;
  justify-content: flex-start;
}

/* ── History ── */
.history-wrap {
  margin-top: 18px;
}

.history-wrap h2 {
  margin: 0 0 10px;
  font-size: 18px;
  transition: color 300ms ease;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 280px;
  overflow: auto;
  display: grid;
  gap: 8px;
}

.history-item {
  display: grid;
  grid-template-columns: 64px 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--surface-strong) 89%, transparent);
  animation: pop 170ms ease-out;
  transition: background 300ms ease, border-color 300ms ease;
}

.history-attempt {
  color: var(--muted);
  font-size: 12px;
}

.history-guess {
  font-family: "DM Mono", monospace;
  font-size: 17px;
  letter-spacing: 0.1em;
}

.history-result {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.result-bulls,
.result-cows {
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 700;
}

.result-bulls {
  color: var(--bull);
  border: 1px solid color-mix(in srgb, var(--bull) 55%, var(--line));
  background: color-mix(in srgb, var(--bull) 14%, transparent);
}

.result-cows {
  color: var(--cow);
  border: 1px solid color-mix(in srgb, var(--cow) 55%, var(--line));
  background: color-mix(in srgb, var(--cow) 14%, transparent);
}

.history-empty {
  border: 1px dashed color-mix(in srgb, var(--line) 80%, var(--primary) 20%);
  border-radius: 12px;
  padding: 12px;
  color: color-mix(in srgb, var(--text) 75%, var(--muted));
  font-size: 13px;
  text-align: center;
  background: color-mix(in srgb, var(--surface-strong) 75%, transparent);
}

/* ── Win / lose animations ── */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
  20%, 40%, 60%, 80% { transform: translateX(4px); }
}

.card.shake {
  animation: shake 500ms ease-in-out;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ── Responsive ── */
@media (max-width: 680px) {
  .app {
    place-items: start center;
  }

  .card {
    display: flex;
    flex-direction: column;
    padding: 18px;
  }

  .input-row {
    flex-direction: column;
  }

  button[type="submit"] {
    width: 100%;
  }

  .inline-new-game.visible {
    width: 100%;
  }

  .actions {
    display: flex;
  }

  .actions .secondary {
    width: 100%;
  }

  .history-list {
    max-height: none;
    overflow-y: visible;
  }

  .rules {
    order: 20;
    margin-top: 14px;
  }
}

@media (max-width: 520px) {
  .top-bar {
    align-items: flex-start;
  }

  .top-bar-right {
    flex-direction: column;
    align-items: flex-end;
    gap: 6px;
  }

  .attempts-panel {
    flex-direction: column;
    align-items: flex-start;
  }

  .history-item {
    grid-template-columns: 42px 1fr;
  }

  .history-result {
    grid-column: 2;
  }

  .rules-list li {
    line-height: 1.45;
  }
}
