:root {
  --desk: #e9e0cd;
  --panel: #f7f1e3;
  --paper: #fffdf4;
  --paper-edge: #d9cead;
  --paper-ink: #3a3020;
  --ink: #33291f;
  --muted: #8a7d68;
  --accent: #e4572e;
  --accent-soft: rgba(228, 87, 46, 0.14);
  --good: #2f9e44;
  --bad: #e03131;
  --hole: #3a3223;
  --shadow: rgba(60, 45, 20, 0.18);
  --btn-bg: #fffdf4;
  --btn-border: #cfc3a4;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #201c15;
    --panel: #2a251c;
    --paper: #f2ead4;
    --paper-edge: #c9bd99;
    --paper-ink: #3a3020;
    --ink: #ece4d0;
    --muted: #a89a80;
    --accent: #ff6b3d;
    --accent-soft: rgba(255, 107, 61, 0.18);
    --hole: #14100a;
    --shadow: rgba(0, 0, 0, 0.45);
    --btn-bg: #352f23;
    --btn-border: #55492f;
  }
}

/* Manual override from the settings menu (independent of OS preference). */
:root[data-theme="dark"] {
  --desk: #201c15;
  --panel: #2a251c;
  --paper: #f2ead4;
  --paper-edge: #c9bd99;
  --paper-ink: #3a3020;
  --ink: #ece4d0;
  --muted: #a89a80;
  --accent: #ff6b3d;
  --accent-soft: rgba(255, 107, 61, 0.18);
  --hole: #14100a;
  --shadow: rgba(0, 0, 0, 0.45);
  --btn-bg: #352f23;
  --btn-border: #55492f;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--desk);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

/* ---------- header ---------- */

header {
  border-bottom: 2px solid var(--btn-border);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

header h1 {
  margin: 0;
  font-size: clamp(1.4rem, 4.5vw, 2rem);
  letter-spacing: 0.14em;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 1px;
  cursor: pointer;
  user-select: none;
}
header h1:hover { color: var(--accent); }
header h1:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 6px; }

.hole-o {
  display: inline-block;
  width: 0.72em;
  height: 0.72em;
  margin: 0 0.05em;
  border-radius: 50%;
  background: var(--hole);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.6), 0 1px 0 rgba(255,255,255,0.35);
  position: relative;
}

/* Punch the "O" hole in whenever you land on / return to the home screen. */
.hole-o.punching {
  animation: holePunch 0.5s cubic-bezier(0.2, 1.5, 0.35, 1) both;
}
.hole-o.punching::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  pointer-events: none;
  animation: holeRipple 0.55s ease-out both;
}
header h1.recoil { animation: titleRecoil 0.4s ease-out; }

@keyframes holePunch {
  0%   { transform: scale(0); }
  55%  { transform: scale(1.28); }
  72%  { transform: scale(0.88); }
  100% { transform: scale(1); }
}
@keyframes holeRipple {
  0%   { transform: scale(0.4); opacity: 0.85; }
  100% { transform: scale(2.8); opacity: 0; }
}
@keyframes titleRecoil {
  0%   { transform: translateY(0) scale(1); }
  28%  { transform: translateY(1px) scale(0.985); }
  100% { transform: translateY(0) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .hole-o.punching,
  .hole-o.punching::after,
  header h1.recoil { animation: none; }
}

.header-side {
  display: flex;
  gap: 8px;
  min-width: 84px;
}
.header-side.right { justify-content: flex-end; }

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 2px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn svg { display: block; width: 18px; height: 18px; }

/* ---------- banner ---------- */

#banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--accent-soft);
  font-size: 0.9rem;
}
#banner button {
  border: none;
  background: none;
  color: var(--accent);
  font-weight: 700;
  cursor: pointer;
  text-decoration: underline;
  font-size: 0.9rem;
}

/* ---------- layout ---------- */

main {
  flex: 1;
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr) 190px;
  gap: 22px;
  align-items: start;
  padding: 44px 14px 30px;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}

#leftCol {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 190px;
  flex-shrink: 0;
}

#rightCol {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-width: 0;
}

.panel {
  background: var(--panel);
  border: 2px solid var(--btn-border);
  border-radius: 14px;
  padding: 12px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.panel-note {
  margin-top: 8px;
  font-size: 0.82rem;
  color: var(--muted);
}

/* Center the heading, grid, and note inside the "Today's pattern" box.
   The grid is display:inline-grid, so text-align centers it too. */
#targetPanel {
  text-align: center;
}

/* ---------- mini grids (target / history / figures / reveal) ---------- */

.mini-wrap { display: inline-grid; gap: 3px; }
.mini-wrap.center { margin: 10px auto; display: grid; width: fit-content; }

.mini-cell {
  width: 26px;
  height: 26px;
  background: var(--paper);
  border: 1px solid var(--paper-edge);
  border-radius: 4px;
  position: relative;
}

.mini-wrap.big .mini-cell {
  width: 44px;
  height: 44px;
  border-radius: 6px;
}

.mini-cell .hole {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 58%;
  height: 58%;
  border-radius: 50%;
  background: var(--hole);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.55);
}

.mini-cell .hole.good { outline: 3px solid var(--good); }
.mini-cell .hole.bad { outline: 3px solid var(--bad); }
.mini-cell .miss-ring {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 58%;
  height: 58%;
  border-radius: 50%;
  border: 2px dashed var(--bad);
  opacity: 0.75;
}

.mini-wrap.big .hole.pop {
  animation: pop 0.35s cubic-bezier(0.2, 1.6, 0.4, 1) backwards;
}

@keyframes pop {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

/* ---------- history ---------- */

#historyList {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.history-item {
  display: flex;
  align-items: center;
  gap: 10px;
}
.history-item .label {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.3;
}
.history-item .mini-cell { width: 15px; height: 15px; border-radius: 3px; }
.history-item .mini-wrap { gap: 2px; }
.history-item .mini-cell .hole.good { outline-width: 2px; }
.history-item .mini-cell .hole.bad { outline-width: 2px; }
.history-item .miss-ring { border-width: 1.5px; }

/* ---------- status ---------- */

#statusBar {
  display: flex;
  gap: 18px;
  font-size: 0.92rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--muted);
}
#statusBar strong { color: var(--ink); }
#attemptNo.status-win strong, #attemptNo.status-win { color: var(--good); }
#attemptNo.status-lose strong, #attemptNo.status-lose { color: var(--bad); }

/* ---------- paper area ---------- */

#playArea {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#paperWrap {
  position: relative;
}

.paper {
  position: absolute;
  display: grid;
  filter: drop-shadow(3px 4px 6px var(--shadow));
}

.paper.settle { animation: settle 0.22s ease-out; }
@keyframes settle {
  from { transform: scale(0.965); }
  to { transform: scale(1); }
}

.pcell {
  background: var(--paper);
  border: 1px solid var(--paper-edge);
  position: relative;
  cursor: pointer;
  color: var(--paper-ink);
}
.pcell:hover { background: #fff8e2; }

.pcell .layers {
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--muted);
  pointer-events: none;
}

.pcell.willfold { background: var(--accent-soft); }

.pcell.sel { outline: 3px solid var(--accent); outline-offset: -2px; z-index: 2; }
.pcell.sel::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 52%;
  height: 52%;
  border-radius: 50%;
  border: 3px dashed var(--accent);
}

.pcell.punched::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 55%;
  height: 55%;
  border-radius: 50%;
  border: none;
  background: var(--hole);
  box-shadow: inset 0 2px 5px rgba(0,0,0,0.6);
  animation: pop 0.25s ease-out;
}

.foldline {
  position: absolute;
  z-index: 5;
  pointer-events: none;
}
.foldline.v { border-left: 2px dashed var(--accent); width: 0; }
.foldline.h { border-top: 2px dashed var(--accent); height: 0; }

/* fold buttons */

.fgroup {
  position: absolute;
  display: flex;
  gap: 2px;
  z-index: 6;
}
.fgroup.h { flex-direction: column; }

.fbtn {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  border: 1.5px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  font-weight: 800;
}
.fbtn:hover:not(:disabled) {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.fbtn:disabled { opacity: 0.25; cursor: default; }

/* ---------- controls ---------- */

#controls {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

button {
  font-family: inherit;
}

#controls button, .result-actions button, .modal > button.primary {
  padding: 10px 16px;
  border-radius: 10px;
  border: 2px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font-weight: 700;
  font-size: 0.92rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
#controls button svg, .result-actions button svg { display: block; width: 16px; height: 16px; }

button.primary {
  background: var(--accent) !important;
  border-color: var(--accent) !important;
  color: #fff !important;
}
button.primary:disabled {
  opacity: 0.4;
  cursor: default;
}
#controls button:not(.primary):hover, .result-actions button:not(.primary):hover {
  border-color: var(--accent);
}

#hintLine {
  font-size: 0.84rem;
  color: var(--muted);
  margin: 12px 0 0;
  text-align: center;
  max-width: 340px;
}

/* ---------- reveal ---------- */

#revealArea {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding-top: 8px;
}

#revealCaption {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  font-weight: 700;
}

#revealVerdict {
  font-size: 1.05rem;
  font-weight: 700;
  text-align: center;
}

.reveal-legend {
  display: flex;
  gap: 14px;
  font-size: 0.78rem;
  color: var(--muted);
  flex-wrap: wrap;
  justify-content: center;
}
.reveal-legend .dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: -1px;
}
.dot.good { background: var(--good); }
.dot.bad { background: var(--bad); }
.dot.miss { border: 2px dashed var(--bad); width: 8px; height: 8px; }

/* ---------- footer ---------- */

footer {
  text-align: center;
  padding: 12px;
  font-size: 0.78rem;
  color: var(--muted);
}

/* ---------- modals ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(30, 22, 10, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}

.modal {
  background: var(--panel);
  border: 2px solid var(--btn-border);
  border-radius: 16px;
  padding: 22px;
  max-width: 440px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
}

.modal h2 { margin: 0 0 10px; }
.modal h3 { margin: 18px 0 8px; font-size: 0.9rem; }
.modal p, .modal li { font-size: 0.92rem; line-height: 1.5; }
.modal ul { padding-left: 20px; }

.modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  border: none;
  background: none;
  font-size: 1.5rem;
  color: var(--muted);
  cursor: pointer;
  line-height: 1;
}

/* ---------- settings / theme switch ---------- */

.theme-switch {
  display: flex;
  gap: 8px;
  margin: 14px 0 4px;
}

.theme-opt {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 8px;
  border-radius: 10px;
  border: 2px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
}
.theme-opt svg { width: 20px; height: 20px; }
.theme-opt:hover { border-color: var(--accent); }
.theme-opt.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.history-item .label .win-mark {
  display: inline-block;
  width: 0.65em;
  height: 0.65em;
  margin-right: 0.35em;
  border-radius: 50%;
  background: var(--good);
  vertical-align: -0.05em;
}

.fig-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 14px 0;
}
.fig { text-align: center; }
.fig .fig-cap {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 6px;
  line-height: 1.35;
}
.fig-grid { display: inline-grid; gap: 2px; position: relative; }
.fig-grid .mini-cell { width: 18px; height: 18px; border-radius: 3px; }
.fig-arrow {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  color: var(--accent);
  font-weight: 800;
  text-shadow: 0 0 6px var(--panel);
  pointer-events: none;
}

.result-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.countdown {
  color: var(--muted);
  font-size: 0.86rem;
  margin-bottom: 0;
}

/* ---------- stats ---------- */

.stats-row {
  display: flex;
  gap: 8px;
  text-align: center;
}
.stats-row .stat { flex: 1; }
.stats-row .stat .num { font-size: 1.6rem; font-weight: 800; }
.stats-row .stat .lbl { font-size: 0.7rem; color: var(--muted); }

.dist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 0.85rem;
}
.dist-row .bar {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
  padding: 2px 8px;
  border-radius: 4px;
  min-width: 20px;
  text-align: right;
}
.dist-row.zero .bar { background: var(--btn-border); color: var(--ink); }

/* ---------- toast ---------- */

#toast {
  position: fixed;
  bottom: 26px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--desk);
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.9rem;
  z-index: 100;
  box-shadow: 0 6px 20px rgba(0,0,0,0.3);
}

/* ---------- responsive ---------- */

@media (max-width: 640px) {
  main {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 14px;
    padding-top: 12px;
  }
  #leftCol {
    flex-direction: row;
    width: auto;
    align-items: flex-start;
    justify-content: center;
    flex-wrap: wrap;
  }
  .mini-cell { width: 20px; height: 20px; }
  .mini-wrap.big .mini-cell { width: 38px; height: 38px; }
}

/* ---------- start screen ---------- */

#startScreen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  /* extra bottom padding biases the vertically-centered stack upward a touch */
  padding: 20px 16px 76px;
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
}

.start-tagline {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.5;
  max-width: 40ch;
}

.mode-grid {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
}

.mode-card {
  font-family: inherit;
  text-align: left;
  background: var(--panel);
  border: 2px solid var(--btn-border);
  border-radius: 16px;
  padding: 18px 20px;
  cursor: pointer;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  gap: 5px;
  transition: transform 0.08s ease, border-color 0.12s ease, box-shadow 0.12s ease;
}
.mode-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 8px 22px var(--shadow);
}
.mode-card:active { transform: translateY(0); }

.mode-card.daily {
  padding: 22px 24px;
  background: linear-gradient(180deg, var(--accent-soft), transparent 65%), var(--panel);
  border-color: var(--accent);
  align-items: flex-start;
}
.mode-card.daily .mode-name { font-size: 1.9rem; }

.mode-eyebrow {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 800;
  color: var(--accent);
}

.mode-name {
  font-size: 1.2rem;
  font-weight: 800;
}
.mode-desc {
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.4;
}

.mode-row {
  display: flex;
  gap: 12px;
}
.mode-card.tier {
  flex: 1;
  min-width: 0;
  align-items: center;
  text-align: center;
  padding: 16px 12px;
  gap: 6px;
  border-top-width: 4px;
}
.mode-card.easy   { border-top-color: var(--good); }
.mode-card.medium { border-top-color: var(--accent); }
.mode-card.hard   { border-top-color: var(--bad); }

.start-foot {
  margin: 2px 0 0;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ---------- multi-punch selection ---------- */

.pcell .punch-num {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--accent);
  pointer-events: none;
}

#attemptNo.status-lose,
#attemptNo.status-lose strong { color: var(--bad); }

@media (max-width: 520px) {
  .mode-row { flex-direction: column; }
  .mode-card.tier {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    text-align: left;
    border-top-width: 2px;
    border-left-width: 6px;
  }
  .mode-card.easy   { border-left-color: var(--good); }
  .mode-card.medium { border-left-color: var(--accent); }
  .mode-card.hard   { border-left-color: var(--bad); }
}
