/* ============================================================================
   FLAG DESIGNER  ·  page stylesheet
   ----------------------------------------------------------------------------
   The tokens copy pages/home/style.css. Desktop is a two-column grid. At 900 px
   and less the control panel becomes a bottom sheet (.panel.sheet-open).

   grep -n targets
     tokens ............... ":root"
     header ............... ".top"
     layout ............... ".layout"
     cards ................ ".card"
     hero cloth ........... ".hero"
     panel and tabs ....... ".panel" / ".tabs"
     thumbnails ........... ".thumb"
     emblems .............. ".emblem-grid"
     shapes ............... ".shape-grid"
     colour picker ........ ".picker" / ".sv" / ".hue"
     segmented buttons .... ".seg"
     sliders .............. ".slider"
     toast ................ ".toast"
     phone sheet .......... "@media (max-width: 900px)"
     touch targets ........ "@media (pointer: coarse)"
     landscape phone ...... "orientation: landscape"
     reduced motion ....... "prefers-reduced-motion"
   ============================================================================ */

:root {
  --bg: #0a0c12; --bg2: #0e1119; --bg3: #131826;
  --ink: #eef3fb; --text: #c3d0e2; --dim: #7f91ad; --faint: #3a4866;
  --line: rgba(150, 200, 255, 0.09); --line2: rgba(150, 200, 255, 0.18);
  --blue: #96c8ff; --gold: #ffc832; --orange: #ff7b00;
  --glass: rgba(16, 21, 33, 0.62);
  --f-sans: 'Space Grotesk', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --f-mono: 'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  --f-serif: 'Instrument Serif', 'Iowan Old Style', Georgia, serif;
  --r: 16px; --r-sm: 10px;
  --ease: cubic-bezier(.2, .7, .2, 1);
  --gutter: clamp(16px, 3vw, 40px);
  --peek: 108px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { background: var(--bg); color-scheme: dark; -webkit-text-size-adjust: 100%; }
body {
  font: 400 15px/1.5 var(--f-sans); color: var(--text); min-height: 100vh; min-height: 100dvh; overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(150, 200, 255, 0.07), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(255, 200, 50, 0.04), transparent 60%),
    var(--bg);
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
[hidden] { display: none !important; }
em { font-family: var(--f-serif); font-style: italic; font-weight: 400; color: var(--gold); }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; border-radius: 8px; }
.kicker, .label, .field-label { font: 500 0.7rem/1.4 var(--f-mono); letter-spacing: 0.16em; text-transform: uppercase; color: var(--blue); opacity: 0.8; }

/* ── header ─────────────────────────────────────────────────────────────── */
.top { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; padding: 28px var(--gutter) 18px; max-width: 1480px; margin: 0 auto; }
.top h1 { font: 300 clamp(1.9rem, 4vw, 3rem)/1.05 var(--f-sans); color: var(--ink); letter-spacing: -0.02em; margin-top: 4px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.btn { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 8px 16px; border-radius: 999px; border: 1px solid var(--line2); font-weight: 500; font-size: 0.9rem; transition: transform .25s var(--ease), box-shadow .3s, background .2s; }
.btn svg { width: 18px; height: 18px; flex: none; }
.btn:active { transform: scale(.97); }
.btn.ghost { background: rgba(20, 26, 42, .82); color: var(--ink); }
.btn.ghost:hover { border-color: rgba(150, 200, 255, .35); }
.btn.gold { background: linear-gradient(135deg, #ffd65c, #ffb31a 55%, #ff8a3d); color: #1a1204; border-color: transparent; box-shadow: 0 8px 26px -10px rgba(255, 190, 40, .6), inset 0 1px 0 rgba(255, 255, 255, .5); }
.btn.gold:hover { box-shadow: 0 12px 34px -10px rgba(255, 190, 40, .8), inset 0 1px 0 rgba(255, 255, 255, .5); }

/* ── layout ─────────────────────────────────────────────────────────────── */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 20px; max-width: 1480px; margin: 0 auto; padding: 0 var(--gutter) 40px; align-items: start; }
.stage { display: flex; flex-direction: column; gap: 20px; min-width: 0; }

.card { position: relative; background: var(--glass); border: 1px solid var(--line); border-radius: var(--r); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.card-head h2 { font: 500 1rem/1 var(--f-sans); color: var(--ink); margin-right: auto; }

/* ── hero cloth ─────────────────────────────────────────────────────────── */
.hero { display: flex; flex-direction: column; }
.hero-wrap { position: relative; height: clamp(360px, 62vh, 640px); }
#flagCanvas { display: block; width: 100%; height: 100%; }
.hero-meta { position: absolute; left: 18px; top: 16px; pointer-events: none; }
.flag-name { font: 300 1.6rem/1.1 var(--f-sans); color: var(--ink); letter-spacing: -0.01em; }
.flag-sub { margin-top: 4px; font: 400 0.74rem/1.4 var(--f-mono); color: var(--dim); letter-spacing: .03em; max-width: 60ch; }
.hero-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 14px; border-top: 1px solid var(--line); }
.wind { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 160px; font: 500 0.7rem/1 var(--f-mono); letter-spacing: .14em; text-transform: uppercase; color: var(--dim); }
.wind input { flex: 1; }

/* ── segmented buttons and chips ────────────────────────────────────────── */
.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 999px; border: 1px solid var(--line2); background: rgba(10, 13, 22, .6); }
.seg button { min-height: 32px; padding: 4px 12px; border-radius: 999px; font: 500 0.8rem/1 var(--f-sans); color: var(--dim); transition: background .2s, color .2s; }
.seg button.on { background: rgba(150, 200, 255, .14); color: var(--ink); box-shadow: inset 0 0 0 1px rgba(150, 200, 255, .3); }
.seg.wrap { display: flex; flex-wrap: wrap; border-radius: 14px; }
.seg.wrap button { flex: 1 0 auto; }
.chip-btn { min-height: 36px; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--line2); background: rgba(20, 26, 42, .7); color: var(--text); font-size: 0.82rem; font-weight: 500; transition: border-color .2s, background .2s; }
.chip-btn:hover { border-color: rgba(150, 200, 255, .35); }
.chip-btn[aria-pressed="true"] { background: rgba(255, 200, 50, .14); border-color: rgba(255, 200, 50, .45); color: var(--gold); }

/* ── panel and tabs ─────────────────────────────────────────────────────── */
.panel { position: sticky; top: 16px; max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; background: var(--glass); border: 1px solid var(--line); border-radius: var(--r); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); overflow: hidden; }
.grip { display: none; }
.tabs { display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; min-height: 40px; border-radius: 10px; font: 500 0.88rem/1 var(--f-sans); color: var(--dim); transition: background .2s, color .2s; }
.tabs button.on { color: var(--ink); background: rgba(150, 200, 255, .1); box-shadow: inset 0 -2px 0 var(--gold); }
.tab-body { padding: 14px; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 14px; }

/* ── thumbnails, emblems and shapes ─────────────────────────────────────── */
.thumb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
.thumb { display: flex; flex-direction: column; gap: 6px; padding: 6px; border-radius: 12px; border: 1px solid var(--line); background: rgba(10, 13, 22, .55); text-align: left; transition: border-color .2s, transform .2s var(--ease); }
.thumb:hover { border-color: rgba(150, 200, 255, .3); transform: translateY(-1px); }
.thumb.on { border-color: var(--gold); box-shadow: 0 0 0 1px rgba(255, 200, 50, .3), 0 6px 18px -8px rgba(255, 200, 50, .5); }
.thumb canvas { width: 100%; aspect-ratio: 3 / 2; border-radius: 6px; }
.thumb span { font: 500 0.74rem/1.25 var(--f-sans); color: var(--text); padding: 0 2px 2px; }
.emblem-tools { display: flex; flex-direction: column; gap: 8px; }
.emblem-tools input[type=search] { min-height: 42px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line2); background: rgba(6, 8, 14, .7); outline: none; color: var(--ink); }
.emblem-tools input[type=search]:focus { border-color: rgba(150, 200, 255, .45); }
.emblem-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 6px; }
.emblem-grid button { position: relative; aspect-ratio: 1; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: rgba(10, 13, 22, .55); display: grid; place-items: center; transition: border-color .2s, background .2s; }
.emblem-grid button:hover { border-color: rgba(150, 200, 255, .3); }
.emblem-grid button.on { border-color: var(--gold); background: rgba(255, 200, 50, .08); }
.emblem-grid canvas { width: 70%; height: 70%; }
.emblem-grid .none { font: 500 0.7rem/1 var(--f-mono); color: var(--dim); letter-spacing: .08em; text-transform: uppercase; }
.empty { font-size: 0.82rem; color: var(--dim); }
.shape-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
.shape-grid button { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 12px 8px 10px; border-radius: 12px; border: 1px solid var(--line); background: rgba(10, 13, 22, .55); font: 500 0.8rem/1 var(--f-sans); color: var(--text); }
.shape-grid button.on { border-color: var(--gold); background: rgba(255, 200, 50, .06); }
.shape-grid svg { width: 72px; height: 48px; }

/* ── colour picker ──────────────────────────────────────────────────────── */
.channels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.channels button { display: flex; flex-direction: column; align-items: center; gap: 6px; min-height: 58px; padding: 8px 4px; border-radius: 12px; border: 1px solid var(--line); background: rgba(10, 13, 22, .55); font-weight: 500; font-size: 0.8rem; color: var(--dim); }
.channels button i { width: 20px; height: 20px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .25); }
.channels button.on { color: var(--ink); border-color: rgba(255, 200, 50, .55); background: rgba(255, 200, 50, .06); }
.picker { display: flex; flex-direction: column; gap: 12px; }
.sv { position: relative; height: 168px; border-radius: 12px; touch-action: none; cursor: crosshair;
  background: linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, hsl(var(--hue, 0), 100%, 50%)); box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .08); }
.hue { position: relative; height: 26px; border-radius: 999px; touch-action: none; cursor: ew-resize;
  background: linear-gradient(to right, #f00, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00); box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .1); }
.knob { position: absolute; width: 22px; height: 22px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .5), 0 2px 8px rgba(0, 0, 0, .5); transform: translate(-50%, -50%); pointer-events: none; }
.hue .knob { top: 50%; width: 26px; height: 26px; }
.hexrow { display: flex; align-items: center; gap: 10px; }
.swatch { width: 44px; height: 44px; border-radius: 12px; flex: none; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .2); }
.hex { flex: 1; display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 12px; border-radius: 12px; border: 1px solid var(--line2); background: rgba(6, 8, 14, .7); }
.hex span { font: 500 0.7rem/1 var(--f-mono); color: var(--dim); letter-spacing: .12em; }
.hex input { flex: 1; min-width: 0; background: none; border: 0; outline: none; font: 500 1rem/1 var(--f-mono); color: var(--ink); letter-spacing: .06em; text-transform: uppercase; }
.quick { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.quick button { aspect-ratio: 1; border-radius: 8px; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .18); min-height: 30px; transition: transform .15s; }
.quick button:hover { transform: scale(1.08); }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── sliders and toggles ────────────────────────────────────────────────── */
.slider { display: flex; flex-direction: column; gap: 8px; }
.slider > span { display: flex; justify-content: space-between; font-size: 0.86rem; color: var(--text); }
.slider b { font: 500 0.78rem/1.4 var(--f-mono); color: var(--gold); }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; touch-action: pan-y; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 4px; background: var(--line2); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--line2); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; margin-top: -9px; border-radius: 50%; background: var(--ink); box-shadow: 0 0 0 4px rgba(150, 200, 255, .18); }
input[type=range]::-moz-range-thumb { width: 22px; height: 22px; border: 0; border-radius: 50%; background: var(--ink); }
.toggle { display: flex; align-items: center; gap: 10px; min-height: 40px; font-size: 0.88rem; }
.toggle input { width: 20px; height: 20px; accent-color: var(--gold); }
.hint { font-size: 0.8rem; color: var(--dim); line-height: 1.5; }

/* ── toast ──────────────────────────────────────────────────────────────── */
.toast { position: fixed; left: 50%; top: 18px; transform: translate(-50%, -20px); opacity: 0; pointer-events: none; z-index: 60; padding: 10px 16px; border-radius: 999px; background: rgba(16, 21, 33, .95); border: 1px solid var(--line2); color: var(--ink); font-size: 0.86rem; transition: opacity .25s, transform .25s var(--ease); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ── phone: bottom sheet ────────────────────────────────────────────────── */
@media (max-width: 900px) {
  :root { --gutter: 16px; }
  body { padding-bottom: calc(var(--peek) + env(safe-area-inset-bottom)); }
  .top { padding-top: 18px; padding-bottom: 12px; align-items: center; }
  .top h1 { font-size: 1.7rem; }
  .btn { padding: 8px 12px; }
  .btn.ghost span { display: none; }
  .actions { flex-wrap: nowrap; }
  .kicker { font-size: 0.66rem; letter-spacing: 0.06em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .brand { min-width: 0; }
  .btn { white-space: nowrap; }
  .layout { grid-template-columns: minmax(0, 1fr); gap: 14px; padding-bottom: 20px; }
  .stage { gap: 14px; }
  .hero-wrap { height: 370px; }
  .hero-bar .wind { order: 3; flex-basis: 100%; }
  .hero-bar .chip-btn { margin-left: auto; }
  .flag-name { font-size: 1.2rem; }
  .flag-sub { font-size: 0.68rem; }
  .tabs button { font-size: 0.8rem; }
  .thumb-grid, .shape-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }

  .panel { position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 40; max-height: none; height: max(360px, 52vh); height: max(360px, 52dvh);
    border-radius: 20px 20px 0 0; border-bottom: 0; background: rgba(12, 16, 26, .94);
    box-shadow: 0 -18px 40px -12px rgba(0, 0, 0, .7);
    transform: translateY(calc(100% - var(--peek) - env(safe-area-inset-bottom)));
    transition: transform .35s var(--ease); padding-bottom: env(safe-area-inset-bottom); }
  .panel.sheet-open { transform: translateY(0); }
  .panel.dragging { transition: none; }
  .grip { display: flex; justify-content: center; align-items: center; height: 44px; flex: none; touch-action: none; cursor: grab; }
  .grip span { width: 44px; height: 5px; border-radius: 5px; background: var(--line2); }
  .tabs { padding-top: 0; }
  .tab-body { flex: 1; min-height: 0; }
  .sv { height: 120px; }
  .toast { top: auto; bottom: calc(var(--peek) + 14px + env(safe-area-inset-bottom)); white-space: nowrap; max-width: calc(100vw - 32px); overflow: hidden; text-overflow: ellipsis; }
}

@media (prefers-reduced-motion: reduce) {
  .panel, .toast { transition: none; }
}

input:disabled, button:disabled { opacity: .4; cursor: default; }

/* ── touch targets ──────────────────────────────────────────────────────────
   On a touch screen each control gets a 44 px hit area. The hue bar keeps
   its 26 px look; its ::before adds 10 px of hit area above and below.
   Inputs use 16 px text, so that iOS Safari does not zoom on focus.        */
@media (pointer: coarse) {
  .tabs button, .seg button, .chip-btn, .toggle { min-height: 44px; }
  .hue::before { content: ''; position: absolute; left: 0; right: 0; top: -10px; bottom: -10px; }
  input, select, textarea { font-size: 16px; }
  input[type=range] { height: 44px; }
}

/* ── landscape phone: side panel ────────────────────────────────────────────
   A landscape phone has too little height for a bottom sheet. The panel
   goes back to a column at the right, and it fills the viewport height.   */
/* A narrow phone hides the export icon, so that "Export PNG" stays on one line. */
@media (max-width: 400px) { .btn.gold svg { display: none; } }

@media (max-width: 900px) and (orientation: landscape) and (max-height: 520px) {
  body { padding-bottom: 0; }
  .top { padding-top: 10px; padding-bottom: 8px; padding-left: max(var(--gutter), env(safe-area-inset-left)); }
  .layout { grid-template-columns: minmax(0, 1fr) min(360px, 46vw); padding-bottom: 8px; padding-left: max(var(--gutter), env(safe-area-inset-left)); padding-right: max(var(--gutter), env(safe-area-inset-right)); }
  .panel, .panel.sheet-open { position: sticky; top: 8px; z-index: auto; height: calc(100vh - 16px); height: calc(100dvh - 16px);
    transform: none; border-radius: var(--r); border-bottom: 1px solid var(--line); padding-bottom: max(0px, calc(env(safe-area-inset-bottom) - 8px)); box-shadow: none; }
  .grip { display: none; }
  .tabs { padding-top: 8px; }
  .hero-wrap { height: max(220px, calc(100dvh - 76px)); }
  .hero-bar .wind { order: 0; flex-basis: auto; }
  .hero-bar .chip-btn { margin-left: 0; }
  .toast { bottom: calc(14px + env(safe-area-inset-bottom)); }
}
