/* Layout: the header holds the difficulty and a menu button. The puzzle stage takes the rest of
   the screen. Pictures, reshuffle and album upload live in a bottom sheet opened from the menu.
   Colours are tokens on :root; dark mode redefines the same tokens. */
:root {
  --bg: #f4efe7;
  --card: #fffdf9;
  --fg: #2b2622;
  --muted: #8c8178;
  --accent: #d9643a;
  --accent-ink: #ffffff;
  --line: #e7dfd3;
  --shadow: rgba(60, 40, 20, 0.16);
  --hl: rgba(217, 100, 58, 0.22);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15141a;
    --card: #1f1e26;
    --fg: #f1ece4;
    --muted: #9b958c;
    --accent: #f08a5d;
    --accent-ink: #1a120d;
    --line: #33313b;
    --shadow: rgba(0, 0, 0, 0.5);
    --hl: rgba(240, 138, 93, 0.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15141a;
  --card: #1f1e26;
  --fg: #f1ece4;
  --muted: #9b958c;
  --accent: #f08a5d;
  --accent-ink: #1a120d;
  --line: #33313b;
  --shadow: rgba(0, 0, 0, 0.5);
  --hl: rgba(240, 138, 93, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  line-height: 1.5;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-top: max(10px, env(safe-area-inset-top, 0px));
  padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

.app { max-width: 440px; margin: 0 auto; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; padding: 0;
}

/* ---- header: title, difficulty and menu on one row ---- */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  position: sticky; top: 0; z-index: 10; background: var(--bg);
  padding: 4px 0 6px; margin: 0;
}
.top h1 { margin: 0; font-size: 20px; letter-spacing: 0.02em; white-space: nowrap; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.seg { display: grid; grid-template-columns: repeat(3, auto); gap: 4px; }

/* ---- stats: one compact row ---- */
.stats { display: flex; gap: 6px; margin-bottom: 10px; }
.stat {
  flex: 1; min-width: 0; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 5px 4px; text-align: center;
}
.stat b {
  display: block; font-size: 16px; font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.stat span { font-size: 11px; color: var(--muted); letter-spacing: 0.04em; }

/* ---- stage: fixed size, the same as the canvas (340 x 670), not scaled ---- */
.stage {
  position: relative;
  width: 340px;
  height: 670px;
  max-width: 100%;
  margin: 0 auto;
}
#cv {
  position: absolute; inset: 0;
  display: block; width: 100%; height: 100%;
  touch-action: none; user-select: none; -webkit-user-select: none;
  cursor: grab;
}
.overlay {
  position: absolute; inset: 0; z-index: 5;
  display: flex; align-items: center; justify-content: center;
  border-radius: 14px;
}
#win { background: rgba(20, 16, 12, 0.55); }
#win .card {
  background: var(--card); color: var(--fg); border-radius: 16px;
  padding: 22px 24px; text-align: center; width: min(80%, 300px);
  box-shadow: 0 16px 40px -12px var(--shadow);
}
#win h2 { margin: 0 0 6px; font-size: 22px; }
#win p { margin: 0 0 14px; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
/* the preview covers the picture area of the board, not the tray; tap to close it */
#preview {
  cursor: pointer;
  inset: auto; left: 2.941%; top: 1.493%; width: 94.118%; aspect-ratio: 1;
}
#preview img { width: 100%; height: 100%; object-fit: cover; border-radius: 14px; display: block; }

.hint { display: none; font-size: 12px; color: var(--muted); margin: 10px 2px 0; line-height: 1.6; }
@media (min-height: 900px) { .hint { display: block; } }

/* ---- buttons ---- */
button, .chip {
  font: inherit; font-size: 14px; color: var(--fg); background: var(--card);
  border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px;
  cursor: pointer; min-height: 40px;
}
button:focus-visible, .chip:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
button[aria-pressed="true"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.seg button { min-height: 34px; padding: 6px 10px; font-size: 13px; }
.menu-btn { min-height: 34px; padding: 6px 14px; font-size: 13px; }

/* ---- bottom sheet (menu) ---- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 19;
  background: rgba(20, 16, 12, 0.4);
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  max-width: 440px; margin: 0 auto;
  background: var(--card); color: var(--fg);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -12px 32px -12px var(--shadow);
  padding: 14px 16px max(16px, env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: 12px;
  max-height: 70vh; overflow-y: auto;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head b { font-size: 16px; }
.close-btn { min-height: 34px; padding: 6px 12px; font-size: 13px; }
.label { margin: 0; font-size: 12px; color: var(--muted); letter-spacing: 0.04em; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; white-space: nowrap; }
.chip[aria-pressed="true"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.chip.upload { cursor: pointer; border-style: solid; color: var(--fg); }
.chip.upload:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

.actions { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.actions button, .actions .chip { width: 100%; padding: 8px 6px; font-size: 13px; justify-content: center; }

.msg { margin: 0; font-size: 13px; color: var(--accent); }
.msg:empty { display: none; }
.row { display: flex; justify-content: center; }

@media (prefers-reduced-motion: reduce) {
  #cv { transition: none; }
}
