/* Photo Puzzle Studio — dual design system.
   Light = "Hearth" (warm cream, terracotta + sage, Source Serif / Karla, rounded).
   Dark  = "Atelier" (near-black gallery, gold accent, Cormorant / Archivo, sharp).
   Themes flip via [data-theme] on <html>. */

:root,
:root[data-theme="light"] {
  color-scheme: light;
  --font-sans: "Karla", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-serif: "Source Serif 4", Georgia, "Times New Roman", serif;

  --page-bg: #e7e0d4;
  --app-bg:
    radial-gradient(circle at 14% 12%, rgba(194, 97, 63, 0.07), transparent 38%),
    radial-gradient(circle at 88% 88%, rgba(124, 138, 106, 0.10), transparent 42%),
    #f6efe3;

  --ink: #3b2e25;
  --ink-soft: #5d4f44;
  --muted: #9b8a7c;
  --muted-2: #7a6a5d;
  --line: rgba(59, 46, 37, 0.18);
  --line-soft: rgba(59, 46, 37, 0.10);

  --surface: #fffaf2;
  --surface-2: #f6efe3;
  --surface-sunken: #e7dcca;

  --accent: #c2613f;
  --accent-hover: #b1542f;
  --accent-strong: #a84e30;
  --accent-contrast: #fff7ef;
  --accent-soft: rgba(194, 97, 63, 0.12);
  --accent-2: #7c8a6a;
  --accent-2-soft: rgba(124, 138, 106, 0.16);

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 12px;
  --pill: 999px;
  --btn-radius: 14px;

  --shadow-lg: 0 40px 90px rgba(59, 46, 37, 0.26);
  --shadow-md: 0 20px 46px rgba(59, 46, 37, 0.12);
  --shadow-sm: 0 8px 20px rgba(59, 46, 37, 0.07);

  --topbar-bg: rgba(255, 250, 242, 0.84);
  --toolbar-bg: rgba(255, 250, 242, 0.9);
  --canvas-wrap-bg: linear-gradient(135deg, #efe6d6, #f6efe3);

  --btn-transform: none;
  --btn-spacing: 0;
  --btn-size: 16px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --font-sans: "Archivo", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-serif: "Cormorant Garamond", Georgia, "Times New Roman", serif;

  --page-bg: #080a0d;
  --app-bg: #0e0f12;

  --ink: #f1ece3;
  --ink-soft: #cfc8ba;
  --muted: #8b8579;
  --muted-2: #a59f93;
  --line: rgba(241, 236, 227, 0.20);
  --line-soft: rgba(241, 236, 227, 0.10);

  --surface: rgba(241, 236, 227, 0.05);
  --surface-2: #121317;
  --surface-sunken: #000000;

  --accent: #c9a66b;
  --accent-hover: #d8b87f;
  --accent-strong: #d8b87f;
  --accent-contrast: #16140f;
  --accent-soft: rgba(201, 166, 107, 0.14);
  --accent-2: #c9a66b;
  --accent-2-soft: rgba(201, 166, 107, 0.12);

  --radius-lg: 4px;
  --radius-md: 3px;
  --radius-sm: 2px;
  --pill: 2px;
  --btn-radius: 2px;

  --shadow-lg: 0 50px 120px rgba(0, 0, 0, 0.7);
  --shadow-md: 0 30px 60px rgba(0, 0, 0, 0.55);
  --shadow-sm: 0 10px 24px rgba(0, 0, 0, 0.5);

  --topbar-bg: rgba(14, 15, 18, 0.86);
  --toolbar-bg: rgba(14, 15, 18, 0.9);
  --canvas-wrap-bg: #0e0f12;

  --btn-transform: uppercase;
  --btn-spacing: 0.14em;
  --btn-size: 13px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--app-bg);
  color: var(--ink);
  font-family: var(--font-sans);
  transition: background 0.4s ease, color 0.3s ease;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Topbar ---------- */

.topbar {
  min-height: 88px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 14px clamp(18px, 3vw, 44px);
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-mark {
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.22);
  object-fit: cover;
}

.brand-name {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.18rem;
  letter-spacing: 0.01em;
  color: var(--ink);
}

.eyebrow {
  margin: 0;
  color: var(--accent-2);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.topbar h1 {
  margin: 2px 0 0;
  color: var(--muted);
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  line-height: 1.1;
}

.topbar-actions,
.toolbar-actions,
.button-row,
.upload-actions,
.stat-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* ---------- Theme toggle ---------- */

.theme-toggle {
  display: inline-flex;
  gap: 2px;
  padding: 4px;
  border-radius: var(--pill);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}

.theme-seg {
  width: 40px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: calc(var(--pill) - 2px);
  background: transparent;
  color: var(--muted);
  transition: background 0.2s ease, color 0.2s ease;
}

:root[data-theme="dark"] .theme-seg {
  border-radius: 2px;
}

:root:not([data-theme="dark"]) .theme-seg.sun,
:root[data-theme="light"] .theme-seg.sun,
:root[data-theme="dark"] .theme-seg.moon {
  background: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
}

/* ---------- Screens ---------- */

main {
  flex: 1;
  min-height: 0;
}

.screen {
  display: none;
  min-height: calc(100vh - 88px);
  padding: clamp(16px, 3vw, 38px);
}

.screen.active {
  display: block;
}

.start-screen {
  display: none;
  align-items: center;
}

.start-screen.active {
  display: flex;
}

.start-grid,
.setup-grid {
  width: min(1240px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 24px;
}

.start-grid {
  grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.55fr);
  align-items: stretch;
}

.setup-grid {
  grid-template-columns: minmax(0, 1fr) minmax(330px, 410px);
}

/* ---------- Hero / landing ---------- */

.hero-panel {
  position: relative;
  min-height: clamp(520px, 68vh, 720px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface-sunken);
  box-shadow: var(--shadow-lg);
  isolation: isolate;
}

.hero-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 58% 42%;
}

.hero-overlay {
  position: absolute;
  inset: 0;
  z-index: 1;
  background:
    linear-gradient(90deg, rgba(40, 26, 18, 0.92) 0%, rgba(40, 26, 18, 0.66) 40%, rgba(40, 26, 18, 0.12) 74%),
    linear-gradient(0deg, rgba(40, 26, 18, 0.7) 0%, transparent 52%);
}

:root[data-theme="dark"] .hero-overlay {
  background:
    linear-gradient(90deg, #0e0f12 0%, #0e0f12 30%, rgba(14, 15, 18, 0.7) 50%, rgba(14, 15, 18, 0.15) 72%, rgba(14, 15, 18, 0.5) 100%);
}

.hero-content {
  position: relative;
  z-index: 2;
  width: min(620px, 92%);
  padding: clamp(30px, 5vw, 58px);
  color: #fff;
}

.hero-content .eyebrow {
  color: var(--accent);
}

:root[data-theme="dark"] .hero-content .eyebrow {
  letter-spacing: 0.42em;
}

.hero-content h2 {
  margin: 12px 0 0;
  max-width: 600px;
  font-family: var(--font-serif);
  font-size: clamp(2.4rem, 4.6vw, 4rem);
  line-height: 1.0;
  font-weight: 600;
  letter-spacing: -0.01em;
}

:root[data-theme="dark"] .hero-content h2 {
  font-weight: 500;
  line-height: 0.94;
}

.hero-copy {
  width: min(420px, 100%);
  margin: 20px 0 0;
  color: rgba(255, 255, 255, 0.82);
  font-size: clamp(1rem, 1.5vw, 1.15rem);
  line-height: 1.65;
}

.hero-panel .upload-actions {
  position: absolute;
  z-index: 2;
  left: clamp(24px, 5vw, 58px);
  right: clamp(24px, 5vw, 58px);
  bottom: clamp(24px, 4vw, 44px);
}

/* ---------- Continue / resume panel ---------- */

.recent-panel,
.settings-panel,
.preview-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}

.recent-panel,
.settings-panel {
  padding: 24px;
}

.recent-panel {
  display: flex;
  flex-direction: column;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.recent-panel h2,
.settings-panel h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--ink);
}

.empty-state {
  min-height: 250px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: var(--radius-md);
  color: var(--muted);
  background: var(--surface-2);
}

.resume-card {
  width: 100%;
  min-height: 156px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 16px;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--accent-soft);
  border-radius: var(--radius-md);
  text-align: left;
  box-shadow: var(--shadow-sm);
}

.resume-card span {
  color: var(--muted-2);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 800;
}

.resume-card strong {
  font-family: var(--font-serif);
  color: var(--accent-2);
  font-size: 1.9rem;
  font-weight: 600;
  line-height: 1;
}

/* ---------- Setup ---------- */

.settings-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.preview-panel {
  min-height: 440px;
  overflow: hidden;
  background: var(--surface-sunken);
  padding: 0;
}

.preview-panel canvas {
  width: 100%;
  height: 100%;
  display: block;
}

fieldset {
  margin: 0;
  padding: 0;
  border: 0;
}

legend {
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.settings-panel > div h2 {
  margin: 0;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
}

.segmented-control label {
  min-height: 58px;
  display: grid;
  place-items: center;
  gap: 2px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--ink);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.15rem;
  text-align: center;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.segmented-control .tier-sub {
  font-family: var(--font-sans);
  font-size: 0.66rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.segmented-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented-control label:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

:root[data-theme="dark"] .segmented-control label:has(input:checked) {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.segmented-control label:has(input:checked) .tier-sub {
  color: var(--accent-2);
}

:root[data-theme="dark"] .segmented-control label:has(input:checked) .tier-sub {
  color: rgba(22, 20, 15, 0.7);
}

.range-row,
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
}

.range-row input {
  width: min(220px, 54vw);
  accent-color: var(--accent-2);
}

.toggle-row {
  justify-content: flex-start;
  font-weight: 600;
  color: var(--ink);
}

.toggle-row input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent-2);
}

/* ---------- Buttons ---------- */

.primary-action,
.secondary-action,
.text-action,
.icon-button {
  min-height: 46px;
  border-radius: var(--btn-radius);
  font-family: var(--font-sans);
  font-weight: 700;
  text-transform: var(--btn-transform);
  letter-spacing: var(--btn-spacing);
  font-size: var(--btn-size);
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.primary-action {
  padding: 0 24px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 0 14px 26px rgba(0, 0, 0, 0.22);
}

.primary-action:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.secondary-action,
.icon-button {
  padding: 0 18px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
}

.secondary-action:hover,
.icon-button:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.hero-panel .secondary-action {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(14px);
}

.hero-panel .secondary-action:hover {
  border-color: #fff;
  color: #fff;
}

.text-action {
  padding: 0 4px;
  min-height: 40px;
  border: 0;
  background: transparent;
  color: var(--accent-strong);
  text-decoration: underline;
  text-underline-offset: 4px;
  text-decoration-thickness: 1.5px;
  text-transform: none;
  letter-spacing: 0;
}

.hero-panel .text-action {
  color: #fff;
}

.hint-button {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.sound-button.is-off {
  color: var(--muted);
  background: var(--surface-2);
}

/* ---------- Puzzle screen ---------- */

.puzzle-screen {
  height: calc(100vh - 88px);
  padding: 0;
  overflow: hidden;
}

.game-toolbar {
  height: 62px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px clamp(12px, 2vw, 24px);
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
}

.stat-strip span {
  min-width: 76px;
  color: var(--muted-2);
  font-weight: 700;
}

.stat-strip #pieceCountLabel {
  font-family: var(--font-serif);
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--ink);
}

.toolbar-actions .icon-button {
  min-height: 44px;
  padding: 0 14px;
}

.canvas-wrap {
  position: relative;
  height: calc(100% - 62px);
  background: var(--canvas-wrap-bg);
  touch-action: none;
}

#puzzleCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.complete-banner {
  position: absolute;
  inset: 0;
  margin: auto;
  width: min(420px, calc(100% - 32px));
  height: fit-content;
  padding: 28px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--accent-soft);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(8px);
}

.complete-banner h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 2.1rem;
  font-weight: 600;
  color: var(--ink);
}

.complete-banner p {
  margin: 10px 0 20px;
  font-size: 1.1rem;
  color: var(--muted-2);
}

/* ---------- Preview dialog ---------- */

dialog {
  width: min(760px, calc(100vw - 28px));
  padding: 0;
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  color: var(--ink);
  box-shadow: var(--shadow-lg);
}

dialog::backdrop {
  background: rgba(8, 8, 10, 0.7);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.dialog-header h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 600;
}

#previewImage {
  width: 100%;
  max-height: 72vh;
  display: block;
  object-fit: contain;
  background: var(--surface-sunken);
}

/* ---------- Gallery (parents' home) ---------- */

.gallery-screen {
  width: min(1240px, 100%);
  margin: 0 auto;
}

.gallery-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 22px;
}

.gallery-head h2 {
  margin: 4px 0 0;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: clamp(1.8rem, 3vw, 2.6rem);
  color: var(--ink);
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: clamp(14px, 2vw, 22px);
}

.gallery-tile {
  position: relative;
  display: block;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  text-align: left;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.gallery-tile:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-lg);
}

.gallery-tile .tile-photo {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  display: block;
  background: var(--surface-sunken);
}

.gallery-tile .tile-body {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
}

.gallery-tile .tile-caption {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.15rem;
  color: var(--ink);
}

.gallery-tile .tile-sub {
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.gallery-tile .tile-badge {
  flex: 0 0 auto;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--accent-2);
}

.gallery-tile .tile-badge.done {
  color: var(--accent-strong);
}

.gallery-empty {
  margin-top: 10px;
}

.gallery-status {
  margin-top: 16px;
  color: var(--muted);
  font-weight: 700;
}

/* ---------- Responsive ---------- */

@media (max-width: 940px) {
  .start-grid,
  .setup-grid {
    grid-template-columns: 1fr;
  }

  .hero-panel {
    min-height: 620px;
  }

  .hero-overlay {
    background: linear-gradient(180deg, rgba(40, 26, 18, 0.9) 0%, rgba(40, 26, 18, 0.66) 44%, rgba(40, 26, 18, 0.4) 100%);
  }

  :root[data-theme="dark"] .hero-overlay {
    background: linear-gradient(180deg, rgba(14, 15, 18, 0.9) 0%, rgba(14, 15, 18, 0.7) 44%, rgba(14, 15, 18, 0.45) 100%);
  }

  .preview-panel {
    min-height: 300px;
  }
}

@media (max-width: 640px) {
  .topbar {
    min-height: 78px;
    padding: 12px 14px;
  }

  .brand-mark {
    width: 42px;
    height: 42px;
  }

  .topbar h1 {
    display: none;
  }

  #installButton,
  #newPuzzleTopButton {
    display: none;
  }

  .screen {
    min-height: calc(100vh - 78px);
    padding: 12px;
  }

  .hero-panel {
    min-height: 650px;
  }

  .hero-content {
    width: 100%;
    padding: 26px;
  }

  .hero-content h2 {
    font-size: clamp(2.1rem, 10vw, 3rem);
  }

  .hero-panel .upload-actions {
    left: 20px;
    right: 20px;
    bottom: 20px;
    display: grid;
    grid-template-columns: 1fr;
  }

  .segmented-control {
    grid-template-columns: 1fr;
  }

  .upload-actions .primary-action,
  .upload-actions .secondary-action,
  .settings-panel .primary-action,
  .settings-panel .secondary-action {
    width: 100%;
  }

  .puzzle-screen {
    height: calc(100vh - 78px);
  }

  .game-toolbar {
    height: auto;
    min-height: 92px;
    align-items: flex-start;
    flex-direction: column;
  }

  .canvas-wrap {
    height: calc(100% - 92px);
  }

  .toolbar-actions {
    width: 100%;
    justify-content: space-between;
  }

  .toolbar-actions .icon-button {
    flex: 1;
    padding: 0 8px;
  }
}
