:root {
  --bg: #0f1116;
  --panel: #171a21;
  --panel-2: #1f232c;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #98a0b3;
  --accent: #4f8cff;
  --danger: #ff6b6b;
  --ok: #4ade80;
  --radius: 10px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html {
  /* Kills the 300ms tap delay and double-tap-to-zoom on controls. */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* The canvas page never scrolls: it fills the screen and stays put, so iOS
   cannot rubber-band or pull-to-refresh it. */
body.app-page {
  position: fixed;
  inset: 0;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button, input, textarea, select {
  font: inherit;
  color: inherit;
}

.btn {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
  touch-action: manipulation;
}
.btn:hover { background: #262b36; border-color: #3a4150; }
.btn:active { background: #2c323e; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #0b1220; font-weight: 600; }
.btn.primary:hover { background: #6a9dff; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.icon {
  width: 42px;
  height: 42px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
  display: grid;
  place-items: center;
}

/* ---------- layout: canvas page ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  padding-top: calc(10px + var(--safe-t));
  padding-left: calc(16px + var(--safe-l));
  padding-right: calc(16px + var(--safe-r));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.topbar .home {
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border-radius: 8px;
}
.topbar .home:hover { background: var(--panel-2); text-decoration: none; }

.titles { min-width: 0; flex: 1 1 auto; }
.titles h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.titles p {
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats {
  display: flex;
  gap: 8px;
  align-items: center;
  flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
}
.chip {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}
.chip b { color: var(--text); font-weight: 650; }
.chip.empty b { color: var(--danger); }
.chip .dot {
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--ok);
  margin-right: 6px;
  vertical-align: middle;
}

.viewport {
  position: relative;
  flex: 1 1 auto;
  overflow: hidden;
  background:
    repeating-conic-gradient(#15181e 0% 25%, #11141a 0% 50%) 50% / 24px 24px;
  touch-action: none;
  cursor: crosshair;
}
.viewport canvas { display: block; }
.viewport.panning { cursor: grabbing; }

.zoom-controls {
  position: absolute;
  right: calc(12px + var(--safe-r));
  bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.zoom-controls .btn {
  background: rgba(31, 35, 44, .86);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.hint {
  position: absolute;
  left: calc(12px + var(--safe-l));
  bottom: 12px;
  color: var(--muted);
  font-size: 12px;
  background: rgba(15, 17, 22, .8);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 9px;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

.toast {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  opacity: 0;
  transition: opacity .2s;
  pointer-events: none;
  max-width: min(90%, 420px);
  text-align: center;
}
.toast.show { opacity: 1; }
.toast.error { border-color: var(--danger); color: #ffd6d6; }

/* Touch flow: tap a cell to aim, then confirm. A fingertip is far wider
   than a pixel, so placing on the raw tap would miss constantly. */
.confirm-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  padding-left: calc(16px + var(--safe-l));
  padding-right: calc(16px + var(--safe-r));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.confirm-bar[hidden] { display: none; }
.confirm-preview {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  flex: 0 0 auto;
  outline: 1px solid rgba(255, 255, 255, .22);
  outline-offset: -1px;
}
.confirm-coords {
  flex: 1 1 auto;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.confirm-bar .btn { flex: 0 0 auto; }
#confirm-place { min-width: 116px; }

.palette {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-bottom: calc(10px + var(--safe-b));
  padding-left: calc(16px + var(--safe-l));
  padding-right: calc(16px + var(--safe-r));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1 1 auto;
}
.swatch {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  border: 2px solid transparent;
  outline: 1px solid rgba(255, 255, 255, .16);
  outline-offset: -1px;
  cursor: pointer;
  padding: 0;
  transition: transform .1s;
  touch-action: manipulation;
}
.swatch:hover { transform: translateY(-2px); }
.swatch[aria-pressed="true"] {
  border-color: var(--text);
  transform: translateY(-2px);
}

/* ---------- layout: index / admin ---------- */

.page {
  max-width: 900px;
  margin: 0 auto;
  padding: 40px 20px 64px;
  padding-top: calc(40px + var(--safe-t));
  padding-bottom: calc(64px + var(--safe-b));
}
.page h1 { font-size: 24px; margin: 0 0 6px; }
.page .lede { color: var(--muted); margin: 0 0 28px; }

.cards {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
}
.card {
  display: block;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  color: inherit;
  transition: border-color .15s, transform .15s;
}
a.card:hover { border-color: var(--accent); transform: translateY(-2px); text-decoration: none; }
a.card:active { border-color: var(--accent); }
.card h2 { margin: 0 0 4px; font-size: 16px; }
.card p { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.card .meta { display: flex; flex-wrap: wrap; gap: 6px; }
.card .mini-palette { display: flex; gap: 3px; margin-top: 12px; }
.card .mini-palette i { width: 12px; height: 12px; border-radius: 3px; display: block; }

.form { display: grid; gap: 14px; }
.field { display: grid; gap: 5px; }
.field label { font-size: 13px; color: var(--muted); }
.field input, .field textarea {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  width: 100%;
  /* 16px keeps iOS Safari from zooming in when a field is focused. */
  font-size: 16px;
}
.field input:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field small { color: var(--muted); font-size: 12px; }
.row { display: grid; gap: 14px; grid-template-columns: 1fr 1fr; }
.notice {
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  white-space: pre-wrap;
  word-break: break-word;
}
.notice.error { border-color: var(--danger); color: #ffd6d6; }
.notice.ok { border-color: var(--ok); color: #d6ffe4; }

/* ---------- phones ---------- */

@media (max-width: 620px) {
  /* Everything fits one row: the labels inside the chips are dropped and the
     description moves behind a tap on the title, so the board keeps the space. */
  .topbar {
    gap: 8px;
    padding: 8px 12px;
    padding-top: calc(8px + var(--safe-t));
    padding-left: calc(12px + var(--safe-l));
    padding-right: calc(12px + var(--safe-r));
  }
  .topbar .home { width: 32px; height: 32px; font-size: 20px; }
  .titles { flex: 1 1 60px; cursor: pointer; }
  .titles h1 { font-size: 15px; }
  .titles p { display: none; }
  .stats { gap: 6px; }
  .chip { padding: 4px 9px; font-size: 12.5px; }
  .chip-word { display: none; }
  .chip .dot { margin-right: 4px; }
  #budget-chip::before {
    content: "";
    display: inline-block;
    width: 9px;
    height: 9px;
    border-radius: 2px;
    background: var(--accent);
    margin-right: 6px;
    vertical-align: middle;
  }
  #budget-chip.empty::before { background: var(--danger); }
  /* The confirm bar already shows the aimed coordinates. */
  .hint { display: none; }

  .palette { padding: 8px 0; padding-bottom: calc(8px + var(--safe-b)); }
  /* One thumb-friendly row that scrolls sideways, so the board keeps the
     vertical space instead of three rows of swatches. */
  .swatches {
    flex-wrap: nowrap;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    gap: 8px;
    padding: 2px calc(12px + var(--safe-l)) 2px calc(12px + var(--safe-r));
    scrollbar-width: none;
  }
  .swatches::-webkit-scrollbar { display: none; }
  .swatch {
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    border-radius: 9px;
    scroll-snap-align: center;
  }
  .swatch:hover { transform: none; }
  .swatch[aria-pressed="true"] { transform: none; border-width: 3px; }

  .confirm-bar { padding: 10px 12px; gap: 8px; }
  #confirm-place { min-width: 104px; padding: 11px 16px; }
  #confirm-cancel { padding: 11px 14px; }
  .zoom-controls { bottom: 10px; right: calc(10px + var(--safe-r)); gap: 6px; }
  .zoom-controls .btn { width: 40px; height: 40px; }
  .hint { bottom: 10px; left: calc(10px + var(--safe-l)); }

  .row { grid-template-columns: 1fr; }
  .page { padding: 28px 16px 48px; padding-bottom: calc(48px + var(--safe-b)); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
