/* ── Reset & base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
.hidden { display: none !important; }

:root {
  --bg:        #f0f4f8;
  --surface:   #ffffff;
  --primary:   #2563eb;
  --primary-h: #1d4ed8;
  --success:   #16a34a;
  --danger:    #dc2626;
  --muted:     #6b7280;
  --border:    #e5e7eb;
  --text:      #111827;
  --amber:     #f59e0b;
  --radius:    0.5rem;
  --shadow:    0 2px 8px rgba(0,0,0,.08);
}

[data-theme="dark"] {
  --bg:      #111827;
  --surface: #1f2937;
  --muted:   #9ca3af;
  --border:  #374151;
  --text:    #f9fafb;
  --shadow:  0 2px 8px rgba(0,0,0,.4);
}

.btn-icon { background: none; border: none; cursor: pointer; font-size: 1.1rem; padding: 0; line-height: 1; }

/* ── Hard mode toggle ─────────────────────────────────────────── */
.btn-hard {
  background: none;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  font-size: .65rem;
  font-weight: 700;
  letter-spacing: .06em;
  padding: .15rem .5rem;
  color: var(--muted);
  line-height: 1.5;
  transition: border-color .15s, color .15s;
}
.hard-mode-info {
  text-align: center;
  font-size: .8rem;
  color: var(--danger);
  margin-top: -.25rem;
  margin-bottom: .5rem;
}
.btn-hard.active {
  border-color: var(--danger);
  color: var(--danger);
}

/* ── Tile animations ──────────────────────────────────────────── */
.tile { transition: transform 0.15s ease-in, opacity 0.15s ease-in; }
.tile.consuming { transform: scale(0); opacity: 0; pointer-events: none; }

@keyframes tile-appear {
  from { transform: scale(0.6); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}
.tile.appearing { animation: tile-appear 0.18s ease-out; }
.tile.dragging  { opacity: .4; }
.tile.drag-over { outline: 2px dashed var(--primary); outline-offset: 3px; }

.op-picker {
  position: fixed;
  z-index: 300;
  display: flex;
  gap: .4rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: .4rem .5rem;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
}
.op-picker-btn {
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 1.2rem;
  width: 2.6rem;
  height: 2.6rem;
  cursor: pointer;
  transition: background .12s, border-color .12s;
  color: var(--text);
}
.op-picker-btn:hover { background: var(--primary); border-color: var(--primary); color: #fff; }

@keyframes tile-shake {
  0%,  100% { transform: translateX(0); }
  20%        { transform: translateX(-6px); }
  60%        { transform: translateX(6px); }
  80%        { transform: translateX(-3px); }
}
.shake { animation: tile-shake 0.3s ease-in-out; }

@media (prefers-reduced-motion: reduce) {
  .tile, .tile.consuming, .tile.appearing, .shake {
    transition: none;
    animation: none;
  }
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 1rem;
}

#app { width: 100%; max-width: 480px; }

/* ── Header ───────────────────────────────────────────────────── */
header { text-align: center; padding: 1.5rem 0 1rem; }
header h1 { font-size: 2rem; letter-spacing: -.5px; }
.subtitle  { color: var(--muted); font-size: .9rem; margin-top: .25rem; }

/* ── Target ───────────────────────────────────────────────────── */
#target-section {
  text-align: center;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: .75rem;
}
#target-label { font-size: .75rem; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
#target-value { font-size: 3rem; font-weight: 700; line-height: 1.1; }
.difficulty-badge {
  display: inline-block;
  font-size: .75rem;
  font-weight: 600;
  padding: .2rem .6rem;
  border-radius: 2rem;
  margin-top: .35rem;
  text-transform: uppercase;
  letter-spacing: .5px;
}
.difficulty-easy   { background: #dcfce7; color: #16a34a; }
.difficulty-medium { background: #fef9c3; color: #ca8a04; }
.difficulty-hard   { background: #fee2e2; color: #dc2626; }

/* ── Status line ──────────────────────────────────────────────── */
#game-status {
  text-align: center;
  font-size: .85rem;
  color: var(--muted);
  margin-bottom: .75rem;
  min-height: 1.2rem;
}

/* ── Tile grid ────────────────────────────────────────────────── */
#tile-grid {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  margin-bottom: .75rem;
  justify-content: center;
}

/* ── Tiles (original + combined) ──────────────────────────────── */
.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 4.5rem;
  padding: .6rem .75rem;
  background: var(--primary);
  color: #fff;
  border-radius: var(--radius);
  border: 2px solid transparent;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s, transform .1s;
  user-select: none;
}
.tile:hover:not(.consumed) { background: var(--primary-h); transform: translateY(-1px); }
.tile:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; }

/* Selected (first operand chosen) */
.tile.selected {
  border-color: var(--amber);
  box-shadow: 0 0 0 3px rgba(245,158,11,.35);
}

/* Pickable (operator chosen, waiting for second tile) */
.tile.pickable:not(.selected) {
  background: var(--primary-h);
  animation: pulse .9s ease-in-out infinite alternate;
}
@keyframes pulse { to { opacity: .7; } }

/* Consumed (used in a combine) */
.tile.consumed {
  background: var(--border);
  color: var(--muted);
  opacity: .45;
  cursor: default;
  transform: none;
}

/* Combined tile extras */
.tile.combined { background: #1e40af; min-width: 7rem; }
.tile.combined:hover:not(.consumed) { background: #1e3a8a; }
.tile-formula {
  font-size: .72rem;
  opacity: .8;
  margin-bottom: .2rem;
  letter-spacing: .3px;
}
.tile-val {
  font-size: 1.4rem;
  font-weight: 700;
}
.tile.combined .tile-val { font-size: 1.25rem; }

/* Uncombine button */
.btn-uncombine {
  position: absolute;
  top: 3px;
  right: 4px;
  background: none;
  border: none;
  color: rgba(255,255,255,.7);
  font-size: .7rem;
  cursor: pointer;
  line-height: 1;
  padding: 1px 2px;
}
.btn-uncombine:hover { color: #fff; }

/* ── Operator buttons ─────────────────────────────────────────── */
#operator-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: .5rem;
  margin-bottom: .75rem;
}
.op-btn {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 1.2rem;
  padding: .65rem;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.op-btn:hover:not(:disabled):not(.active) { background: var(--bg); }
.op-btn.active {
  background: var(--amber);
  border-color: var(--amber);
  color: #fff;
}
.op-btn:disabled { opacity: .3; cursor: default; }
[data-theme="dark"] .op-btn:not(.active):not(:disabled) { background: #374151; border-color: #6b7280; color: #fff; }

/* ── Submit ───────────────────────────────────────────────────── */
#submit-section { margin-bottom: 1.5rem; }
#submit-preview {
  text-align: center;
  font-size: .85rem;
  color: var(--primary);
  font-weight: 600;
  min-height: 1.3rem;
  margin-bottom: .4rem;
  font-family: monospace;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .1rem;
}
#btn-submit {
  width: 100%;
  background: var(--success);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1.1rem;
  font-weight: 600;
  padding: .85rem;
  cursor: pointer;
  transition: background .15s, opacity .15s;
}
#btn-submit:hover:not(:disabled) { background: #15803d; }
#btn-submit:disabled { opacity: .35; cursor: default; }

/* ── Guess history ────────────────────────────────────────────── */
#guess-history {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.25rem;
}
#guess-history h2 { font-size: 1rem; margin-bottom: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
#guess-list { list-style: none; display: flex; flex-direction: column; gap: .5rem; }
.guess-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: .5rem .75rem;
  border-radius: calc(var(--radius) - 2px);
  background: var(--bg);
  font-size: .9rem;
}
.guess-item.correct { background: #dcfce7; color: #111827; }
[data-theme="dark"] .guess-item.correct { background: #14532d; color: #f0fdf4; }
.guess-steps  { font-family: monospace; display: flex; flex-direction: column; gap: .1rem; }
.guess-step   { font-size: .85rem; }
.guess-meta   { display: flex; flex-direction: column; align-items: flex-end; gap: .1rem; font-size: .8rem; color: var(--muted); flex-shrink: 0; margin-left: .75rem; }
.guess-result { font-weight: 600; }
.guess-score  { color: var(--primary); font-weight: 600; }
[data-theme="dark"] .guess-item.correct .guess-score { color: #86efac; }

/* ── Shared modal overlay + close button ──────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
/* .modal-overlay.hidden handled by global .hidden rule */
.btn-modal-close {
  position: absolute;
  top: .75rem; right: .9rem;
  background: none;
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}
.btn-modal-close:hover { color: var(--text); }

/* ── End screen ───────────────────────────────────────────────── */
#end-screen {
  z-index: 100;
}
#end-card {
  position: relative;
  background: var(--surface);
  border-radius: 1rem;
  padding: 2rem;
  text-align: center;
  max-width: 320px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
#end-title   { font-size: 1.75rem; margin-bottom: .5rem; }
#end-message { color: var(--muted); margin-bottom: 1rem; }
#end-score   { font-size: 2.5rem; font-weight: 700; color: var(--primary); margin-bottom: .5rem; }
.end-stats-strip {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  font-size: .85rem;
  color: var(--muted);
  margin-bottom: .6rem;
}
.end-achievements {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: .4rem;
  margin-bottom: .75rem;
}
.achievement-badge {
  background: var(--amber);
  color: #fff;
  border-radius: 999px;
  padding: .2rem .75rem;
  font-size: .8rem;
  font-weight: 600;
}
.end-solution {
  font-family: monospace;
  font-size: .9rem;
  background: var(--bg);
  border-radius: calc(var(--radius) - 2px);
  padding: .5rem .75rem;
  margin-bottom: .75rem;
  color: var(--text);
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: .15rem;
}
.solution-label {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  margin-bottom: .1rem;
}
.end-hint     { font-size: .85rem; color: var(--muted); }
.end-countdown { font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; margin-top: .25rem; }

/* ── User bar ─────────────────────────────────────────────────── */
#user-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  min-height: 1.5rem;
  margin-top: .35rem;
  font-size: .85rem;
}
#user-greeting { color: var(--muted); cursor: pointer; }
.btn-text {
  background: none;
  border: none;
  color: var(--primary);
  font-size: .85rem;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}
.btn-text:hover { color: var(--primary-h); }

/* ── Auth modal ────────────────────────────────────────────────── */
#auth-modal {
  z-index: 150;
}
#auth-card, #account-card {
  position: relative;
  background: var(--surface);
  border-radius: 1rem;
  padding: 1.75rem 2rem;
  width: 90%;
  max-width: 340px;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
#auth-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  border-bottom: 2px solid var(--border);
}
#btn-auth-close {
  padding: 0 0 .35rem .5rem;
}
#auth-tabs {
  display: flex;
  gap: .25rem;
}
.auth-tab {
  background: none;
  border: none;
  padding: .5rem .75rem;
  font-size: 1rem;
  cursor: pointer;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
}
.auth-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.field { display: flex; flex-direction: column; gap: .3rem; margin-bottom: .9rem; }
.field label { font-size: .8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.field input {
  padding: .55rem .7rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: .95rem;
  outline: none;
  transition: border-color .15s;
}
.field input:focus { border-color: var(--primary); }
.field-optional { font-weight: 400; text-transform: none; letter-spacing: 0; }
.auth-error { color: var(--danger); font-size: .85rem; margin-bottom: .75rem; }
.auth-success { color: var(--success); font-size: .85rem; margin-bottom: .75rem; }
.btn-auth {
  width: 100%;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  padding: .75rem;
  cursor: pointer;
  transition: background .15s;
}
.btn-auth:hover { background: var(--primary-h); }

.end-guest-prompt { font-size: .9rem; color: var(--muted); margin: .5rem 0; }
.btn-link { background: none; border: none; cursor: pointer; color: var(--primary); font-size: inherit; padding: 0; text-decoration: underline; }

.btn-google {
  display: flex; align-items: center; justify-content: center;
  gap: .6rem; width: 100%; padding: .65rem;
  border: 1.5px solid var(--border); border-radius: var(--radius);
  background: #fff; color: var(--text);
  font-size: .95rem; font-weight: 500;
  text-decoration: none; cursor: pointer;
  transition: background .15s;
}
.btn-google:hover { background: var(--bg); }
.auth-or {
  display: flex; align-items: center; gap: .75rem;
  color: var(--muted); font-size: .8rem; margin: .75rem 0;
}
.auth-or::before, .auth-or::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}

/* ── Stats modal ──────────────────────────────────────────────── */
#stats-modal { z-index: 150; }
#stats-card {
  position: relative;
  background: var(--surface);
  border-radius: 1rem;
  padding: 2rem;
  text-align: center;
  max-width: 320px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
#stats-card h2 { font-size: 1.5rem; margin-bottom: 1.25rem; }
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .75rem;
  text-align: center;
}
.stat-cell {
  background: var(--bg);
  border-radius: var(--radius);
  padding: .75rem .5rem;
}
.stat-label {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  margin-bottom: .25rem;
}
.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--primary);
}
.stats-error {
  color: var(--muted);
  font-style: italic;
  font-size: .9rem;
}

/* ── Hints ────────────────────────────────────────────────────── */
#hint-section { margin-bottom: .5rem; }

#hint-steps { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .4rem; }

.hint-step {
  background: var(--surface);
  border: 1px solid var(--amber);
  border-radius: var(--radius);
  padding: .3rem .6rem;
  font-size: .85rem;
  font-family: monospace;
  color: var(--text);
}

#btn-hint {
  background: none;
  border: 1.5px solid var(--amber);
  border-radius: var(--radius);
  color: var(--amber);
  cursor: pointer;
  font-size: .85rem;
  padding: .35rem .75rem;
  transition: background .15s;
}
#btn-hint:hover:not(:disabled) { background: rgba(245,158,11,.1); }
#btn-hint:disabled { opacity: .45; cursor: default; }
.hint-cost { font-size: .75rem; opacity: .85; }

/* ── Share preview + button ───────────────────────────────────── */
.share-preview {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: .9rem;
  line-height: 1.6;
  white-space: pre-wrap;
  background: var(--bg);
  border-radius: var(--radius);
  padding: .75rem 1rem;
  margin-top: .75rem;
  text-align: left;
  color: var(--text);
  border: 1.5px solid var(--border);
}


.btn-share {
  width: 100%;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  padding: .75rem;
  cursor: pointer;
  transition: background .15s;
  margin-top: .75rem;
}
.btn-share:hover { background: var(--primary-h); }

/* ── Archive modal ────────────────────────────────────────────── */
#archive-modal { z-index: 150; }
#archive-card {
  position: relative;
  background: var(--surface);
  border-radius: 1rem;
  padding: 2rem;
  max-width: 320px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
#archive-card h2 { font-size: 1.5rem; margin-bottom: 1rem; }
.archive-row {
  display: flex;
  align-items: center;
  gap: .5rem;
  width: 100%;
  padding: .5rem .25rem;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  text-align: left;
  font-size: .9rem;
  color: var(--text);
}
.archive-row:hover { background: var(--bg); }
.archive-date   { flex: 1; font-family: monospace; }
.archive-status { color: var(--success); font-weight: 600; min-width: 1.2rem; }
.archive-score  { color: var(--primary); font-weight: 600; font-size: .8rem; }
.archive-empty  { color: var(--muted); font-size: .9rem; text-align: center; padding: .75rem 0; }

/* ── Guest archive banner ─────────────────────────────────────── */
.archive-guest-banner {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .6rem .75rem;
  font-size: .85rem;
  color: var(--muted);
  margin-bottom: .75rem;
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: wrap;
}
.archive-guest-banner .btn-text { font-size: .85rem; padding: 0; }

/* ── Archive banner ───────────────────────────────────────────── */
#archive-banner {
  background: var(--amber);
  color: #fff;
  text-align: center;
  padding: .4rem;
  font-size: .85rem;
  border-radius: var(--radius);
  margin-bottom: .5rem;
}

/* ── End screen community stats ───────────────────────────────── */
.end-community-stats {
  margin: .75rem 0 .25rem;
  padding: .75rem 1rem;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.community-stats-title {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: .5rem;
}
.community-stats-row {
  display: flex;
  justify-content: space-around;
  gap: .5rem;
}
.community-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .1rem;
}
.community-stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
}
.community-stat-label {
  font-size: .75rem;
  color: var(--muted);
}
.community-loading {
  font-size: .8rem;
  color: var(--muted);
}

/* ── Account sections ─────────────────────────────────────────── */
.account-section {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}
.account-section-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: .75rem;
}
.danger-title { color: var(--danger); }
.delete-warning {
  font-size: .85rem;
  color: var(--muted);
  margin-bottom: .75rem;
}
.btn-danger {
  width: 100%;
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  padding: .75rem;
  cursor: pointer;
  transition: background .15s;
}
.btn-danger:hover { background: #b91c1c; }
#delete-confirm { margin-top: .75rem; }
#btn-cancel-delete { margin-top: .5rem; display: block; }

/* ── Toast ────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 1.5rem; left: 50%;
  transform: translateX(-50%);
  background: #1f2937;
  color: #f9fafb;
  padding: .6rem 1.2rem;
  border-radius: 2rem;
  font-size: .9rem;
  z-index: 200;
  transition: opacity .3s;
}
#toast.hidden { opacity: 0; pointer-events: none; }
