/* Bricks — daily word game. Follows the system light/dark theme; a header toggle can
   force one via [data-theme="light"|"dark"] on <html>. */
:root {
  --bg: #f3f4f8;
  --surface: #ffffff;
  --text: #14181f;
  --muted: #6b7280;
  --border: #e3e6ee;
  --green: #2ea043;
  --green-dark: #218838;
  --yellow: #d4a72c;
  --yellow-dark: #b8901f;
  --absent: #111827;
  --absent-dark: #000000;
  --baseplate: #2f7d4f;
  --shadow: 0 10px 24px -12px rgba(15, 23, 42, 0.35);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    "Apple Color Emoji", "Segoe UI Emoji";
  /* Element sizes bounded by BOTH width and height so the whole game fits one screen. */
  --tile: min(13vw, 5.2vh, 3rem);          /* history guess tiles */
  --slot-brick: min(16vw, 6.6vh, 3.6rem);  /* bricks in the word slots */
  --tray-brick: min(12.5vw, 5vh, 3rem);    /* bricks in the tray pool */
  --gap: 0.4rem;
}

/* Dark palette — applied when the system is dark (and not forced light), or forced dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #1b1f27;
    --text: #e8eaed;
    --muted: #9aa0a8;
    --border: #2c313b;
    --baseplate: #2a6b45;
    --shadow: 0 10px 24px -12px rgba(0, 0, 0, 0.6);
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #1b1f27;
  --text: #e8eaed;
  --muted: #9aa0a8;
  --border: #2c313b;
  --baseplate: #2a6b45;
  --shadow: 0 10px 24px -12px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(900px 500px at 12% -10%, rgba(46, 160, 67, 0.10), transparent),
    radial-gradient(800px 520px at 110% 0%, rgba(0, 87, 168, 0.10), transparent),
    var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden; /* the game fits one screen; no page scroll */
}

/* Fixed-height column: header + guess board + play area, all on one screen. */
.wrap {
  max-width: 560px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.6rem 0.7rem max(0.6rem, env(safe-area-inset-bottom));
  overflow: hidden;
}

/* ---- Header ---- */
header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.title { display: flex; align-items: baseline; gap: 0.6rem; }
h1 { margin: 0; font-size: 1.6rem; letter-spacing: 0.02em; }
.home-link { color: inherit; text-decoration: none; }
.home-link:hover { text-decoration: underline; text-underline-offset: 3px; }
.home-link:focus-visible { outline: 2px solid var(--green); outline-offset: 3px; border-radius: 4px; }
.date { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.tagline { margin: 0 0 1rem; color: var(--muted); font-size: 0.92rem; }

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.45rem 0.8rem;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.icon-btn:hover { transform: translateY(-1px); }
.header-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

/* ---- History (the 6-guess board) — a stack of letter bricks, not flat Wordle tiles ----
   Each cell renders as a mini brick: two studs poking up from the top, a gradient body,
   and a 3D bottom edge. The colors still encode correct / present / absent. */
#history {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  margin: 0 auto;
  align-items: center;
  padding-top: 4px; /* room for the top row's studs */
}
.history-row { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: var(--gap); }
.mini {
  --mini-color: #787c7e; /* absent letter = gray, matching the bricks */
  position: relative;
  width: var(--tile);
  height: var(--tile);
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: calc(var(--tile) * 0.55);
  color: #fff;
  background:
    linear-gradient(180deg,
      color-mix(in srgb, var(--mini-color), white 22%) 0%,
      var(--mini-color) 45%,
      color-mix(in srgb, var(--mini-color), black 16%) 100%);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.4),
    inset 0 -3px 5px rgba(0, 0, 0, 0.22),
    0 2px 0 color-mix(in srgb, var(--mini-color), black 38%),
    0 4px 7px rgba(0, 0, 0, 0.2);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}
/* Two studs poking up from the top of each mini brick. Use ::before / ::after so the
   markup stays identical to before — purely a CSS upgrade to the existing cells. */
.mini::before,
.mini::after {
  content: "";
  position: absolute;
  top: -0.13em;
  width: 0.34em;
  height: 0.22em;
  border-radius: 50% / 60%;
  background:
    radial-gradient(circle at 35% 30%,
      color-mix(in srgb, var(--mini-color), white 35%) 0 40%,
      var(--mini-color) 75%,
      color-mix(in srgb, var(--mini-color), black 18%) 100%);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}
.mini::before { left: calc(50% - 0.45em); }
.mini::after  { right: calc(50% - 0.45em); }

.mini.correct { --mini-color: var(--green); }
.mini.present { --mini-color: var(--yellow); }
.mini.absent  { --mini-color: var(--absent); } /* eliminated letter = black brick */

/* Future-guess rows: no brick yet, just a ghost outline so the bricks above and
   below it still feel like part of one stack. */
.mini.empty {
  background: transparent;
  box-shadow: none;
  border: 2px dashed var(--border);
  color: transparent;
}
.mini.empty::before,
.mini.empty::after { display: none; }

/* Row being revealed: neutral light brick (still studded) until each tile flips color. */
.mini.pending {
  --mini-color: color-mix(in srgb, var(--muted), white 30%);
  color: #fff;
}

/* ---- Play area: fills remaining height, distributes board / actions / tray ---- */
#play-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 0.6rem;
}

/* ---- Baseplate / slots ---- */
#board { flex: 0 0 auto; display: flex; justify-content: center; }
.baseplate {
  display: grid;
  grid-template-columns: repeat(5, var(--slot-brick));
  gap: var(--gap);
  padding: 0.5rem;
  border-radius: 14px;
  background:
    radial-gradient(circle at 50% 35%, rgba(255, 255, 255, 0.35) 0 18%, transparent 19%),
    var(--baseplate);
  background-size: 22px 22px, auto;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.25), var(--shadow);
}
.slot {
  width: var(--slot-brick);
  height: calc(var(--slot-brick) * 1.2);
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.16);
  box-shadow: inset 0 3px 6px rgba(0, 0, 0, 0.3);
  /* No `perspective` here: it would make the slot a containing block for the dragged
     brick's position:fixed, throwing the drag off. Perspective lives in the flip keyframe. */
}
.slot.drop-hover { outline: 3px dashed rgba(255, 255, 255, 0.8); outline-offset: -3px; }
.slot.green { box-shadow: inset 0 0 0 3px var(--green), inset 0 3px 6px rgba(0, 0, 0, 0.2); }

/* ---- Brick ---- */
.brick {
  --brick-color: #d01217;
  position: relative;
  width: 100%;
  max-width: 84px;
  aspect-ratio: 5 / 6;
  border-radius: 9px;
  padding-top: 0.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
  touch-action: none;
  color: #fff;
  background:
    linear-gradient(180deg,
      color-mix(in srgb, var(--brick-color), white 22%) 0%,
      var(--brick-color) 45%,
      color-mix(in srgb, var(--brick-color), black 16%) 100%);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.45),
    inset 0 -5px 8px rgba(0, 0, 0, 0.28),
    0 4px 0 color-mix(in srgb, var(--brick-color), black 38%),
    0 9px 14px rgba(0, 0, 0, 0.28);
  transition: transform 0.08s ease, box-shadow 0.12s ease;
}
.brick:focus-visible { outline: 3px solid #111; outline-offset: 2px; }
.brick:active { cursor: grabbing; }

/* Brick letter scales with the brick (set per context below), studs/labels in em. */
#slots .brick { font-size: calc(var(--slot-brick) * 0.5); }
#tray .brick { width: var(--tray-brick); max-width: var(--tray-brick); font-size: calc(var(--tray-brick) * 0.5); }

.studs {
  position: absolute;
  top: -0.18em;
  display: flex;
  gap: 0.55em;
}
.studs span {
  width: 0.34em;
  height: 0.22em;
  border-radius: 50% / 60%;
  background:
    radial-gradient(circle at 35% 30%,
      color-mix(in srgb, var(--brick-color), white 35%) 0 40%,
      var(--brick-color) 75%,
      color-mix(in srgb, var(--brick-color), black 18%) 100%);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}

.brick .tile {
  font-weight: 800;
  font-size: 1em;
  line-height: 1;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}
.brick .alt {
  margin-top: 0.12em;
  font-size: 0.4em;
  font-weight: 700;
  opacity: 0.72;
  letter-spacing: 0.04em;
}
.brick .alt::before { content: "⇄ "; opacity: 0.8; }

/* Flip-side letter known (absent/present/correct), shown as a pill so feedback stays visible. */
.brick .alt.state-absent,
.brick .alt.state-present,
.brick .alt.state-correct {
  opacity: 1;
  color: #fff;
  border-radius: 5px;
  padding: 0.02rem 0.3rem;
}
.brick .alt.state-absent { background: var(--absent); }
.brick .alt.state-present { background: var(--yellow); }
.brick .alt.state-correct { background: var(--green); }

/* Locked / solved brick */
.brick.locked {
  --brick-color: var(--green);
  cursor: default;
}
.brick.locked .alt { visibility: hidden; }
.brick.locked .alt.state-absent { visibility: visible; }
.brick.locked::after {
  content: "✓";
  position: absolute;
  top: 3px;
  right: 5px;
  font-size: 0.7rem;
  font-weight: 800;
  opacity: 0.85;
}

/* Dragging — JS sets `transform: translate3d(...) scale rotate` each frame, so the
   element is composited (no layout/repaint of the shadow per move). Pin at the origin
   and kill the transition so it tracks the cursor 1:1 instead of easing behind it. */
.brick.dragging {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  margin: 0;
  transition: none;
  will-change: transform;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.45),
    0 16px 26px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}
body.dragging-active { cursor: grabbing; }

/* ---- Tray ---- */
#tray-wrap {
  flex: 0 0 auto;
  padding: 0.5rem 0.6rem;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.tray-label {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.85rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.4rem;
}
.guess-count { font-variant-numeric: tabular-nums; }
#tray {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
  align-items: center;
  justify-content: center;
}
#tray.drop-hover { outline: 2px dashed var(--green); outline-offset: 4px; border-radius: 10px; }
#tray .brick { flex: 0 0 auto; }

/* ---- Submit ---- */
.actions { flex: 0 0 auto; display: flex; justify-content: center; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.ghost-btn {
  appearance: none;
  font: inherit;
  font-weight: 700;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0.7rem 1.1rem;
  border-radius: 11px;
  cursor: pointer;
  transition: transform 0.08s ease, color 0.12s ease, border-color 0.12s ease;
}
.ghost-btn:hover:not(:disabled) { transform: translateY(-1px); color: var(--text); border-color: var(--muted); }
.ghost-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.ghost-btn.small { padding: 0.3rem 0.7rem; font-size: 0.78rem; text-transform: none; letter-spacing: 0; }
.submit {
  appearance: none;
  border: none;
  font: inherit;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fff;
  background: linear-gradient(180deg, var(--green) 0%, var(--green-dark) 100%);
  padding: 0.6rem 2.2rem;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 5px 0 #186a2c, 0 10px 18px -8px rgba(0, 0, 0, 0.4);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.submit:hover:not(:disabled) { transform: translateY(-1px); }
.submit:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 2px 0 #186a2c; }
.submit:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: 0 5px 0 #186a2c; }

.hint { text-align: center; color: var(--muted); font-size: 0.82rem; margin-top: 0.9rem; }

/* ---- Modals ---- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: grid;
  place-items: center;
  padding: 1rem;
  z-index: 2000;
}
.modal.hidden { display: none; }
.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: 16px;
  padding: 1.4rem;
  box-shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.5);
  position: relative;
}
.modal-card h2 { margin: 0 0 0.5rem; font-size: 1.3rem; }
.modal-card p { margin: 0 0 1rem; color: var(--muted); }
.close-x {
  position: absolute;
  top: 0.7rem;
  right: 0.8rem;
  background: none;
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
}
.share-grid {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 1.4rem;
  line-height: 1.25;
  white-space: pre;
  text-align: center;
  margin: 0 0 1rem;
}
.modal-actions { display: flex; gap: 0.6rem; justify-content: center; flex-wrap: wrap; }
.btn {
  appearance: none;
  border: 1px solid var(--border);
  font: inherit;
  font-weight: 700;
  padding: 0.7rem 1.3rem;
  border-radius: 11px;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
}
.btn.primary {
  border: none;
  color: #fff;
  background: linear-gradient(180deg, var(--green) 0%, var(--green-dark) 100%);
}
.btn:hover { transform: translateY(-1px); }

/* Stats */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  text-align: center;
  margin-bottom: 1.2rem;
}
.stat-grid .num { font-size: 1.6rem; font-weight: 800; }
.stat-grid .lbl { font-size: 0.7rem; color: var(--muted); }
.dist-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-bottom: 0.5rem; }
#dist { display: flex; flex-direction: column; gap: 0.3rem; }
.dist-row { display: flex; align-items: center; gap: 0.5rem; }
.dist-label { width: 1rem; font-weight: 700; font-size: 0.85rem; }
.dist-bar {
  background: var(--green);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.15rem 0.4rem;
  border-radius: 5px;
  min-width: 1.4rem;
  text-align: right;
  box-sizing: border-box;
}

@media (max-width: 380px) {
  .baseplate { gap: 0.35rem; padding: 0.55rem; }
  h1 { font-size: 1.4rem; }
}

@media (max-height: 430px) {
  :root {
    --tile: min(13vw, 4.6vh, 3rem);
    --slot-brick: min(16vw, 5.8vh, 3.6rem);
    --tray-brick: min(12.5vw, 4.4vh, 3rem);
    --gap: 0.25rem;
  }

  .wrap {
    gap: 0.35rem;
    padding: 0.35rem 0.6rem max(0.35rem, env(safe-area-inset-bottom));
  }

  header { gap: 0.5rem; }
  h1 { font-size: 1.2rem; }
  .date { font-size: 0.72rem; }
  .header-actions { gap: 0.3rem; }
  .icon-btn {
    font-size: 0.72rem;
    padding: 0.3rem 0.5rem;
    border-radius: 8px;
  }

  #history { padding-top: 2px; }
  #play-area { gap: 0.25rem; }
  .baseplate {
    padding: 0.35rem;
    border-radius: 10px;
  }
  #tray-wrap {
    padding: 0.25rem 0.45rem;
    border-radius: 10px;
  }
  .tray-label {
    font-size: 0.62rem;
    line-height: 1;
    margin-bottom: 0.2rem;
  }
  .actions { gap: 0.35rem; }
  .ghost-btn {
    padding: 0.45rem 0.75rem;
    border-radius: 9px;
  }
  .ghost-btn.small {
    padding: 0.2rem 0.5rem;
    font-size: 0.68rem;
  }
  .submit {
    padding: 0.4rem 1.6rem;
    border-radius: 10px;
  }
}

/* ---- Flip animation ---- */
/* Single-element "flip over": rotate to edge-on, jump to the opposite edge (both
   points invisible), then rotate flat so the new face turns in from the other side.
   This reads as one continuous flip rather than a wobble. The letter is swapped in
   JS at the ~50% edge-on point. */
.brick.flipping { animation: brick-flip 0.42s ease-in-out; backface-visibility: hidden; }
@keyframes brick-flip {
  0%     { transform: perspective(600px) rotateX(0deg); }
  49.99% { transform: perspective(600px) rotateX(90deg); }
  50%    { transform: perspective(600px) rotateX(-90deg); }
  100%   { transform: perspective(600px) rotateX(0deg); }
}

/* ---- Invalid-word shake ---- */
.baseplate.shake { animation: shake 0.5s; }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-7px); }
  40%, 60% { transform: translateX(7px); }
}

/* ---- Toast ("Not a word") ---- */
.toast {
  position: fixed;
  left: 50%;
  top: 72px;
  transform: translateX(-50%) translateY(-12px);
  background: var(--text);
  color: var(--bg);
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.6rem 1rem;
  border-radius: 10px;
  box-shadow: 0 10px 24px -8px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 3000;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- How to play modal ---- */
.help-lead { margin: 0 0 1rem; color: var(--text); }
.help-list { margin: 0 0 1rem; padding-left: 1.1rem; display: flex; flex-direction: column; gap: 0.6rem; }
.help-list li { color: var(--text); font-size: 0.95rem; line-height: 1.5; }
.help-list strong { font-weight: 700; }
.help-foot { margin: 0; color: var(--muted); font-size: 0.92rem; line-height: 1.5; }

@media (prefers-reduced-motion: reduce) {
  .brick.flipping { animation: none; }
  .baseplate.shake { animation: none; }
  .toast { transition: opacity 0.18s ease; }
  .brick, .submit, .icon-btn, .btn { transition: none; }
}

/* ---- Win celebration overlay ---- */
.win-canvas { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1500; }
.win-flash { position: fixed; inset: 0; background: #fff; pointer-events: none; z-index: 1400; opacity: 0; }
