:root {
  --bg: #f6f5f3;
  --fg: #1c1b19;
  --muted: #6f6b66;
  --card: #ffffff;
  --line: #e4e1dc;
  --empty: #e9e6e1;
  --none: #f6f5f3;
  --accent: #0f766e;
  --accent-fg: #ffffff;
  --danger: #b42318;
  --ok-bg: #e7f5ee;
  --err-bg: #fdecea;
  --info-bg: #e8f1fb;
  --radius: 10px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .18);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131312;
    --fg: #ebe9e6;
    --muted: #9c978f;
    --card: #1d1c1a;
    --line: #302e2b;
    --empty: #2a2826;
    --none: #131312;
    --accent: #2bb5a6;
    --accent-fg: #0b1a18;
    --danger: #f47066;
    --ok-bg: #16302a;
    --err-bg: #3a1c1a;
    --info-bg: #182838;
    --shadow: 0 10px 30px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.4rem; margin: .4rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.grow { flex: 1; min-width: 0; }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: var(--card);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--fg); text-decoration: none; }
.logo {
  width: 18px; height: 18px; border-radius: 4px;
  background: conic-gradient(#009E73 0 25%, #56B4E9 0 50%, #F0E442 0 75%, #D55E00 0);
}
.nav { margin-left: auto; display: flex; gap: 14px; font-size: .9rem; overflow-x: auto; }
.nav a { color: var(--fg); text-decoration: none; white-space: nowrap; }
.nav a:hover { color: var(--accent); }

.main { max-width: 760px; margin: 0 auto; padding: 12px 8px 48px; }
@media (min-width: 480px) { .main { padding: 16px 16px 48px; } }

/* ---- Cards, forms, buttons ---- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin: 0 0 16px;
}
.card.narrow { max-width: 420px; margin: 24px auto; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.inline-form { display: flex; gap: 8px; align-items: flex-end; }
.inline-form.wrap { flex-wrap: wrap; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; font-weight: 500; }
label.check { flex-direction: row; align-items: flex-start; gap: 8px; font-weight: 400; }
label.check input { margin-top: 3px; }
input, select, textarea {
  font: inherit; color: var(--fg); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; min-height: 40px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; }
input[type=color] { width: 44px; height: 40px; padding: 2px; flex: none; cursor: pointer; }
input:disabled { opacity: .6; }
input.num { width: 90px; }
.inline-form input, .inline-form select { width: auto; }
.inline-form input[type=search] { flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 500; text-decoration: none; cursor: pointer;
  min-height: 40px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.block { width: 100%; }
.btn.sm { min-height: 34px; padding: 5px 10px; font-size: .85rem; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 8px; border: 0; background: transparent;
  color: var(--muted); font-size: 1.1rem; cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--empty); color: var(--fg); }

.alert { padding: 10px 12px; border-radius: 8px; margin: 0 0 14px; }
.alert p { margin: 0 0 8px; }
.alert.ok { background: var(--ok-bg); }
.alert.err { background: var(--err-bg); color: var(--danger); }
.alert.info { background: var(--info-bg); }
.badge {
  display: inline-block; font-size: .72rem; font-weight: 600; padding: 1px 7px; border-radius: 99px;
  background: var(--empty); color: var(--muted); vertical-align: middle;
}
.back { margin: 0 0 4px; font-size: .9rem; }
.back a { text-decoration: none; }
.code { white-space: pre-wrap; word-break: break-word; background: var(--bg); padding: 10px; border-radius: 8px; font-size: .8rem; }
.dot { display: inline-block; width: 14px; height: 14px; border-radius: 4px; background: var(--c); flex: none; }

.plain-list { list-style: none; margin: 0 0 12px; padding: 0; }
.plain-list li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.plain-list li:last-child { border-bottom: 0; }
.table-wrap { overflow-x: auto; margin-bottom: 8px; }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.table th { color: var(--muted); font-weight: 500; }
.danger-zone { border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.danger-zone .row + .row { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.danger-zone-inline { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--line); }

/* ---- Board list ---- */
.board-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; margin-bottom: 20px; }
.board-card {
  display: flex; flex-direction: column; gap: 4px; padding: 10px; text-decoration: none; color: var(--fg);
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
}
.board-card:hover { border-color: var(--accent); }
.board-card-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumb { width: 100%; aspect-ratio: 12 / 20; border-radius: 6px; display: block; }
.thumb-bg { fill: var(--empty); }

.templates { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; }
.templates legend { font-size: .9rem; font-weight: 500; margin-bottom: 6px; padding: 0; }
.template { flex-direction: row; align-items: center; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-weight: 400; }
.template:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.template-body { display: flex; flex-direction: column; gap: 4px; }
.template-name { font-weight: 600; }
.template-dots { display: flex; gap: 4px; flex-wrap: wrap; }

/* ---- Board grid ---- */
.page-board .main { max-width: 720px; }
.board-head { display: flex; flex-direction: column; align-items: center; gap: 2px; margin-bottom: 10px; }
.board-name { display: flex; align-items: center; gap: 2px; margin: 0; font-size: 1.25rem; text-align: center; max-width: 100%; }
#boardNameText { cursor: text; overflow-wrap: anywhere; }
.rename-input { font-size: 1.1rem; font-weight: 600; text-align: center; max-width: 280px; }
.year-nav { display: flex; align-items: center; gap: 4px; }
.year { font-weight: 600; font-variant-numeric: tabular-nums; min-width: 3.2em; text-align: center; }

.grid {
  display: grid;
  grid-template-columns: 1.25rem repeat(12, minmax(0, 1fr));
  gap: 2px;
  user-select: none;
  -webkit-user-select: none;
}
.mh, .dl {
  font-size: .68rem; color: var(--muted); display: flex; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums;
}
.mh { padding-bottom: 2px; }
.dl { justify-content: flex-end; padding-right: 3px; }
.c {
  display: block; aspect-ratio: 1; min-height: 22px; width: 100%; padding: 0; margin: 0; border: 0;
  border-radius: 3px; background: var(--c, var(--empty)); cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.c.f { background: var(--c); }
.c:hover:not(:disabled) { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--fg) 35%, transparent); }
.c:focus-visible { outline: 2px solid var(--fg); outline-offset: 1px; }
.c.t { box-shadow: inset 0 0 0 2px var(--fg); }
.c.fu { opacity: .35; cursor: default; }
.c.x {
  background: repeating-linear-gradient(135deg, transparent 0 3px, var(--line) 3px 4px);
  cursor: default;
}
@media (min-width: 768px) {
  .grid { gap: 3px; }
  .mh, .dl { font-size: .78rem; }
  .c { border-radius: 4px; }
}

.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; justify-content: center; margin: 14px 0; font-size: .85rem; }
.chip { display: inline-flex; align-items: center; gap: 6px; }
.board-actions { display: flex; gap: 10px; justify-content: center; }

/* ---- Picker: bottom sheet on mobile, popover on desktop ---- */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .25); z-index: 10; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11;
  background: var(--card); border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  transform: translateY(100%); transition: transform .18s ease-out;
}
.sheet.open { transform: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.swatches { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.swatch {
  min-height: 48px; border: 0; border-radius: 10px; padding: 6px; font: inherit; font-size: .85rem; font-weight: 600;
  cursor: pointer; overflow-wrap: anywhere;
}
.swatch.on { box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--fg); }
.sheet-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; min-height: 40px; }
@media (min-width: 768px) {
  .backdrop { background: transparent; }
  .sheet {
    right: auto; bottom: auto; width: 320px; border-radius: 12px; padding: 12px;
    transform: none; opacity: 0; transition: opacity .12s;
  }
  .sheet.open { opacity: 1; }
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 20;
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: .9rem;
  box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

/* ---- Palette editor ---- */
.item-list { list-style: none; padding: 0; margin: 0 0 12px; }
.item { padding: 10px 0; border-bottom: 1px solid var(--line); }
.item-row { display: flex; align-items: center; gap: 8px; }
.item-row input[name=label] { flex: 1; min-width: 0; }
.item-tools { display: flex; align-items: center; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.item-tools > .muted { margin-right: auto; }
.item-tools form { display: flex; gap: 6px; }
.item-tools select { width: auto; min-height: 34px; padding: 4px 8px; font-size: .85rem; }
.item.archived .item-row { min-height: 34px; }
.add-item { margin-top: 4px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.save-status { font-size: .8rem; color: var(--muted); min-height: 1.2em; text-align: right; }
.save-status.ok { color: var(--accent); }
.save-status.err { color: var(--danger); }
.drag-handle {
  flex: none; width: 32px; height: 40px; border: 0; border-radius: 6px; padding: 0;
  background: transparent; color: var(--muted); font-size: 1.2rem; line-height: 1;
  cursor: grab; touch-action: none; -webkit-user-select: none; user-select: none;
}
.drag-handle:hover, .drag-handle:focus-visible { background: var(--empty); color: var(--fg); }
.item.dragging { opacity: .85; background: var(--empty); border-radius: 8px; box-shadow: var(--shadow); position: relative; z-index: 1; }
.item.dragging .drag-handle { cursor: grabbing; }
#activeList .item-tools { padding-left: 40px; }
.swatch-btn {
  flex: none; width: 40px; height: 40px; padding: 0; cursor: pointer;
  border: 1px solid var(--line); border-radius: 8px; box-shadow: inset 0 0 0 2px var(--card);
}
.swatch-btn[aria-expanded=true] { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- Hex color picker (colorpicker.js) ---- */
.hexpicker {
  position: absolute; z-index: 30; width: min(260px, calc(100vw - 16px));
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  padding: 10px; display: flex; flex-direction: column; gap: 10px;
}
.hp-sv { position: relative; height: 150px; border-radius: 8px; cursor: crosshair; touch-action: none; }
.hp-hue {
  position: relative; height: 14px; border-radius: 7px; cursor: pointer; touch-action: none;
  background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00);
}
.hp-thumb {
  position: absolute; width: 16px; height: 16px; border-radius: 50%; pointer-events: none;
  border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .5); transform: translate(-50%, -50%);
}
.hp-hue .hp-thumb { top: 50%; background: transparent; }
.hp-row { display: flex; gap: 8px; align-items: center; }
.hp-preview { width: 40px; height: 40px; border-radius: 8px; border: 1px solid var(--line); flex: none; }
.hp-hex { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; text-transform: uppercase; letter-spacing: .04em; }
.hp-presets { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.hp-presets button { aspect-ratio: 1; border: 0; border-radius: 6px; cursor: pointer; padding: 0; }
.hp-presets button.on { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--fg); }

/* ---- Members: add-by-search, roles ---- */
.member-add { display: flex; gap: 8px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 4px; }
.member-add select { width: auto; }
.combo { position: relative; flex: 1 1 200px; }
.suggest {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 20; list-style: none; margin: 0; padding: 4px;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow);
}
.suggest li { padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.suggest li.on, .suggest li:hover { background: var(--empty); }
.suggest li.empty { color: var(--muted); cursor: default; background: none; }
.member select { width: auto; min-height: 34px; padding: 4px 8px; font-size: .85rem; }
.badge.role-owner { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }
.roles-help { margin-top: 8px; font-size: .85rem; }
.roles-help summary { cursor: pointer; color: var(--accent); }
.roles-help dl { margin: 8px 0 0; display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; }
.roles-help dt { font-weight: 600; }
.roles-help dd { margin: 0; color: var(--muted); }
.grid.readonly .c { cursor: default; }
.grid.readonly .c:hover { box-shadow: none; }
