/* ==========================================================================
   1. reset, base styles and variable system
   ========================================================================== */

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root, [data-theme="light"] {
  --bg: #ffffff;
  --bg2: #fafafa;
  --bg3: #f5f5f5;
  --card-bg: #f5f5f5;
  --gold: #171717;
  --gold-dim: #525252;
  --cream: #0a0a0a;
  --muted: #5c5c5c;
  --text: #0a0a0a;
  --border: rgba(0, 0, 0, 0.1);
  --border-light: rgba(0, 0, 0, 0.08);
  --nav-bg: rgba(255, 255, 255, 0.97);
  --focus-ring: rgba(23, 23, 23, 0.35);
  --radius: 10px;
  --radius-sm: 6px;
  --danger: #b91c1c;
}

[data-theme="dark"] {
  --bg: #0a0a0a;
  --bg2: #141414;
  --bg3: #1f1f1f;
  --card-bg: rgba(255, 255, 255, 0.04);
  --gold: #e5e5e5;
  --gold-dim: #737373;
  --cream: #fafafa;
  --muted: #9a9a9a;
  --text: #fafafa;
  --border: rgba(255, 255, 255, 0.08);
  --border-light: rgba(255, 255, 255, 0.07);
  --nav-bg: rgba(10, 10, 10, 0.97);
  --focus-ring: rgba(229, 229, 229, 0.4);
  --danger: #ef4444;
}

html {
  height: 100%;
  scroll-behavior: smooth;
  scrollbar-width: thin;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', sans-serif;
  font-weight: 400;
  line-height: 1.7;
  font-size: 16px;
  transition: background 0.3s ease, color 0.3s ease;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Page layout flows naturally (scrolls if content + footer exceed
   viewport height) rather than locking to a fixed viewport, so the
   footer always has room below the board on every screen size. */

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

a:focus-visible, button:focus-visible, input:focus-visible, .theme-toggle:focus-visible, .hamburger:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
  border-radius: 4px;
}

h1 { font-family: 'Playfair Display', serif; font-weight: 700; }
h2 { font-family: 'Playfair Display', serif; font-weight: 600; }

/* ==========================================================================
   2. navigation
   ========================================================================== */

.topnav {
  position: sticky;
  top: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  gap: 36px;
  padding: 18px 60px;
  background: var(--nav-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topnav .brand {
  font-family: 'Playfair Display', serif;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold);
  margin-right: auto;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 36px;
}

.topnav a {
  position: relative;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding-bottom: 4px;
  transition: color 0.2s;
  white-space: nowrap;
}
.topnav a::after {
  content: '';
  position: absolute;
  left: 0; bottom: -2px;
  width: 100%; height: 1.5px;
  background: var(--gold);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.25s ease;
}
.topnav a:hover, .topnav a.active { color: var(--gold); }
.topnav a:hover::after, .topnav a.active::after { transform: scaleX(1); }

.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  cursor: pointer;
  width: 46px; height: 26px;
  border-radius: 13px;
  position: relative;
  transition: border-color 0.2s;
  flex-shrink: 0;
}
.theme-toggle:hover { border-color: var(--gold); }
.toggle-thumb {
  position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--gold);
  transition: transform 0.25s ease;
}
[data-theme="dark"] .toggle-thumb { transform: translateX(20px); }
.t-icon { position: absolute; font-size: 9px; top: 50%; transform: translateY(-50%); pointer-events: none; transition: opacity 0.2s; }
.t-sun { right: 6px; opacity: 1; }
.t-moon { left: 6px; opacity: 0; color: var(--text); }
[data-theme="dark"] .t-sun { opacity: 0; }
[data-theme="dark"] .t-moon { opacity: 1; color: var(--cream); }

/* Hamburger button, hidden on desktop */
.hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  width: 34px;
  height: 34px;
  padding: 0;
  margin: 0;
  border: none;
  background: none;
  cursor: pointer;
  flex-shrink: 0;
}
.hamburger-bar {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--gold);
  border-radius: 2px;
  transition: transform 0.25s ease, opacity 0.2s ease;
}
.hamburger.open .hamburger-bar:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.hamburger.open .hamburger-bar:nth-child(2) { opacity: 0; }
.hamburger.open .hamburger-bar:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.mobile-menu {
  display: flex;
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  height: 100dvh;
  width: min(78vw, 300px);
  background: var(--nav-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-left: 1px solid var(--border);
  z-index: 900;
  flex-direction: column;
  gap: 4px;
  padding: 90px 28px 28px;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.12);
  transform: translateX(100%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.28s ease;
}

/* The hamburger button turns into an X when open, but the panel's
   z-index (900) sits above the navbar (500), covering it — so that X
   is invisible and untappable. This close button lives inside the
   panel itself, in the same top-right spot the hamburger/toggle
   occupy in the navbar, so there's always a visible, reachable way
   to dismiss the menu. */
.mobile-menu-close {
  position: absolute;
  top: 16px;
  right: 20px;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  border: none;
  background: none;
  color: var(--gold);
  cursor: pointer;
  flex-shrink: 0;
}
.mobile-menu-close:hover { opacity: 0.75; }

.mobile-menu a {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 14px 4px;
  border-bottom: 1px solid var(--border-light);
}
.mobile-menu a.active, .mobile-menu a:hover { color: var(--gold); }

.mobile-menu-overlay {
  display: block;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 800;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.28s ease;
}

.mobile-menu.open {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}
.mobile-menu-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

@media (max-width: 720px) {
  .nav-links { display: none; }
  .hamburger { display: flex; }
  .topnav { padding: 16px 20px; gap: 16px; }
}

/* ==========================================================================
   3. page layout
   ========================================================================== */

main {
  max-width: 480px;
  margin: 0 auto;
  padding: 24px 20px;
  text-align: center;
}

h1 { font-size: clamp(1.4rem, 2.6vw, 1.9rem); margin-bottom: 4px; letter-spacing: -0.01em; color: var(--cream); }
.subtitle { color: var(--muted); font-size: 0.88rem; margin-top: 0; margin-bottom: 18px; }

main.recognize-page {
  max-width: 100%;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

@media (max-width: 899px), (max-height: 759px) {
  main.recognize-page {
    overflow: visible;
    justify-content: flex-start;
    padding-top: 20px;
    padding-bottom: 32px;
  }
}

@media (max-width: 480px) {
  main { padding: 18px 14px; }
  .board-row { gap: 16px; }
  #drawCanvas, #testDrawCanvas { width: 240px; height: 240px; }
  .network-viz-wrap { width: 100%; max-width: 320px; }
}

/* ==========================================================================
   3b. mobile flow: draw -> act -> result, with the network visualization
   demoted to a secondary section below rather than competing for the
   same space between the drawing pad and the Predict/Clear buttons.
   .board-row's own box is removed from layout (display:contents) so its
   children become direct flex items of main.recognize-page and can be
   reordered together with #controls and #statusMessage, which live
   outside .board-row in the markup.
   ========================================================================== */
@media (max-width: 720px), (max-height: 759px) {
  main.recognize-page { align-items: stretch; }
  main.recognize-page > h1,
  main.recognize-page > .subtitle { text-align: center; }

  main.recognize-page .board-row { display: contents; }

  main.recognize-page .draw-canvas-wrap { order: 1; }
  main.recognize-page #controls { order: 2; }
  main.recognize-page .result-card { order: 3; }
  main.recognize-page #statusMessage { order: 4; }
  main.recognize-page .network-viz-wrap { order: 5; margin-top: 8px; }
  main.recognize-page .story-spacer { display: none; }

  /* The "How it was made" dropdown is position:absolute on desktop,
     which is safe there because its siblings sit beside it in a flex
     row, not below it. On mobile everything stacks vertically, so an
     absolutely-positioned dropdown would float over whatever comes
     next (the Predict/Clear buttons) instead of pushing them down.
     Putting it back in normal flow here means it reserves its own
     space and pushes later content down like any other block. */
  main.recognize-page .draw-canvas-wrap { position: static; }
  main.recognize-page .model-story { position: static; margin-top: 4px; }

  main.recognize-page .draw-canvas-wrap,
  main.recognize-page .result-card,
  main.recognize-page .network-viz-wrap { align-self: center; }

  main.recognize-page #controls {
    display: flex;
    justify-content: center;
    width: 100%;
    max-width: 280px;
    margin: 4px auto 0;
  }
  main.recognize-page #controls button { min-width: 140px; margin: 0; }

  main.recognize-page .result-card {
    width: 100%;
    max-width: 280px;
    height: auto;
    min-height: 120px;
    padding: 18px;
    margin-top: 14px;
  }

  main.recognize-page .network-viz-wrap::before {
    content: "Network activity";
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
  }

  /* Same draw -> act -> result flow as the recognizer page, applied to
     the "Test Your Model" section of the training lab. There's no
     .draw-canvas-wrap here (testDrawCanvas is a bare canvas, no model
     name/story dropdown alongside it), so it's targeted directly. */
  .test-model-section {
    display: flex;
    flex-direction: column;
    align-items: stretch;
  }
  .test-model-section .board-row { display: contents; }

  .test-model-section #testDrawCanvas { order: 1; align-self: center; }
  .test-model-section #testControls { order: 2; }
  .test-model-section .result-card { order: 3; align-self: center; }
  .test-model-section #testStatusMessage { order: 4; }
  .test-model-section .network-viz-wrap { order: 5; margin-top: 8px; align-self: center; }

  .test-model-section #testControls {
    display: flex;
    justify-content: center;
    width: 100%;
    max-width: 280px;
    margin: 16px auto 0;
  }
  .test-model-section #testControls button { min-width: 140px; margin: 0; }

  .test-model-section .result-card {
    width: 100%;
    max-width: 280px;
    height: auto;
    min-height: 120px;
    padding: 18px;
    margin-top: 14px;
  }

  .test-model-section .network-viz-wrap::before {
    content: "Network activity";
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
  }
}

/* The hide-until-first-prediction behavior for the test result card is
   a mobile-only affordance, same reasoning as the recognizer page's
   result card above — always show it on larger screens. */
@media (min-width: 721px) and (min-height: 760px) {
  .test-model-section .result-card[hidden] { display: flex; }
}

.board-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}

#drawCanvas,
#testDrawCanvas {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #000;
  cursor: crosshair;
  touch-action: none;
  width: 280px;
  height: 280px;
  max-width: 100%;
}

.network-legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 12px;
  padding: 8px 10px;
  margin: 0;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  background: var(--card-bg);
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}
.network-legend li { display: flex; align-items: center; gap: 5px; }
.network-legend .dot {
  width: 9px; height: 9px; border-radius: 50%;
  box-shadow: 0 0 4px currentColor;
  flex-shrink: 0;
}

#controls, #testControls { margin-top: 16px; }

button {
  padding: 10px 20px;
  margin: 0 5px;
  font-family: 'Inter', sans-serif;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.2s, opacity 0.2s, background 0.2s;
}
button:hover { border-color: var(--gold); opacity: 0.85; }

button.primary { background: var(--gold); color: var(--bg); border-color: var(--gold); }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }

.mode-toggle {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin: 0 0 18px;
}
.mode-toggle button {
  margin: 0;
  padding: 8px 22px;
}
.mode-toggle button.active {
  background: var(--gold);
  color: var(--bg);
  border-color: var(--gold);
}

.result-card {
  width: 280px;
  height: 280px;
  max-width: 100%;
  padding: 22px;
  border-radius: var(--radius);
  background: var(--card-bg);
  border: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}
.result-card[hidden] { display: none; }

/* The hidden-until-first-prediction behavior is a mobile-only affordance
   (see the 720px/759px media query below) to avoid an empty result card
   eating space above the fold. On larger screens the result card is a
   permanent fixture of the three-panel layout, so it should always be
   visible, prediction or not — override the [hidden] attribute here. */
@media (min-width: 721px) and (min-height: 760px) {
  main.recognize-page .result-card[hidden] { display: flex; }
}

.network-viz-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#networkCanvas,
#testNetworkCanvas {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #000;
  cursor: default;
  width: 540px;
  max-width: 100%;
  height: 420px;
}

@supports (aspect-ratio: 1080 / 840) {
  #networkCanvas,
  #testNetworkCanvas {
    height: auto;
    aspect-ratio: 1080 / 840;
  }
}

.result-digit { font-family: 'Playfair Display', serif; font-size: 48px; font-weight: 700; color: var(--cream); }
.result-confidence { font-size: 0.78rem; color: var(--muted); margin-top: 6px; letter-spacing: 0.04em; }

#statusMessage, #testStatusMessage { min-height: 18px; font-size: 0.78rem; color: var(--gold-dim); margin-top: 16px; }

.train-page { max-width: 1220px; width: 100%; text-align: left; overflow-x: hidden; overflow-y: auto; }
.train-page h1, .train-page .subtitle { text-align: center; }

.train-panels-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  align-items: start;
}
@media (max-width: 720px) {
  .train-panels-row { grid-template-columns: 1fr; }
}

.train-panels-row .panel,
.model-panel {
  text-align: center;
}
.train-panels-row .panel #drawCanvas,
.train-panels-row .panel .digit-grid,
.train-panels-row .panel .stats-list {
  margin-left: auto;
  margin-right: auto;
}
.train-panels-row .panel label { text-align: center; }

.model-panel .board-row { gap: 14px; align-items: center; }
.model-name-input {
  padding: 10px 14px;
  font-family: 'Inter', sans-serif;
  font-size: 0.82rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  min-width: 200px;
}
.model-name-input:focus { outline: none; border-color: var(--gold); }

.test-model-section {
  text-align: center;
  margin-bottom: 20px;
}
.test-model-section h2 {
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold);
  margin-top: 0;
  margin-bottom: 16px;
  font-weight: 600;
}

.draw-canvas-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.model-name {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: center;
}

.model-story {
  width: 100%;
  max-width: 280px;
}
.model-story summary {
  cursor: pointer;
  font-size: 0.72rem;
  color: var(--muted);
  text-align: center;
  letter-spacing: 0.04em;
  list-style: none;
  padding: 4px 0;
  transition: color 0.2s;
}
.model-story summary::-webkit-details-marker { display: none; }
.model-story summary::before {
  content: "+ ";
  color: var(--gold);
}
.model-story[open] summary::before {
  content: "\2212 ";
}
.model-story summary:hover { color: var(--text); }
.draw-canvas-wrap { position: relative; }
.model-story { position: absolute; top: 100%; left: 0; right: 0; z-index: 50; }
.model-story-link {
  display: block;
  margin-top: 10px;
  border-radius: var(--radius);
  transition: opacity 0.2s, transform 0.2s;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
}

.model-story-link:hover,
.model-story-link:focus-visible {
  opacity: 0.85;
  transform: scale(1.01);
}
.story-spacer { height: 0; transition: height 0.2s ease; }
main:has(.model-story[open]) .story-spacer { height: 168px; }
.model-story img {
  display: block;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #000;
  pointer-events: none;
}

.panel-hint {
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.5;
  margin: -8px 0 14px;
}

.result-card {
  animation: result-fade-in 0.28s ease;
}
@keyframes result-fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .result-card { animation: none; }
}

.panel {
  background: var(--card-bg);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 20px;
}

.panel h2 {
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold);
  margin-top: 0;
  margin-bottom: 16px;
  font-weight: 600;
}

.digit-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-top: 14px;
  list-style: none;
  padding: 0;
}
.digit-grid button { margin: 0; }

.stats-list {
  list-style: none;
  padding: 0;
  margin: 0 0 14px;
  font-size: 0.82rem;
  color: var(--muted);
  columns: 2;
}

.log-box {
  background: var(--bg3);
  color: var(--muted);
  font-family: 'Inter', monospace;
  font-size: 0.75rem;
  padding: 12px;
  border-radius: var(--radius-sm);
  max-height: 180px;
  overflow-y: auto;
  white-space: pre-wrap;
  border: 1px solid var(--border-light);
}

.train-page label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 12px;
}

.train-page input {
  display: block;
  margin-top: 6px;
  padding: 8px 10px;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', sans-serif;
  -webkit-appearance: none;
  appearance: none;
  font-size: 16px; /* prevents iOS auto-zoom on focus */
}
.train-page input[hidden] { display: none; }

a, button, input, textarea, select {
  touch-action: manipulation;
}

/* ==========================================================================
   FOOTER SIGNATURE & BRAND COMPONENT
   ========================================================================== */

footer {
  padding: 32px 60px;
  background: var(--bg);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--border);
  transition: background 0.3s, border-color 0.3s;
  flex-wrap: wrap;
  gap: 12px;
}
footer p {
  font-size: 0.72rem;
  color: var(--muted);
  letter-spacing: 0.06em;
  line-height: 1.6;
}
.footer-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.footer-logo {
  font-family: 'Playfair Display', serif;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold);
}

.footer-icons {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin: 10px 0;
}
.footer-icon-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  color: var(--muted);
  transition: color 0.2s, border-color 0.2s, transform 0.2s;
}
.footer-icon-link:hover {
  color: var(--text);
  border-color: var(--gold);
  transform: translateY(-2px);
}

@media (max-width: 640px) {
  footer { padding: 24px; flex-direction: column; text-align: center; gap: 10px; }
  .footer-left { align-items: center; }
}