/* ─── SmartWiki base ─────────────────────────────────────────────
   Neutral tokens + shared components for the dashboard and every wiki.
   Accent, fonts, radius and width are NOT defined here: js/theme.js derives
   them per wiki (--accent, --accent-ink, --accent-soft, --btn-bg, --font-*,
   --radius*, --content-width). Neutrals are scoped by [data-theme] so a
   preview box can carry its own light/dark inside the page. */

:root, [data-theme="light"] {
  --bg: #FFFFFF;
  --surface: #FFFFFF;
  --surface-2: #F6F7F9;
  --surface-3: #EDEFF3;
  --side-bg: #FAFAFB;
  --border: #E6E8EC;
  --border-strong: #8A919C;            /* 3.2:1 on white — interactive boundaries */
  --text: #111827;
  --text-muted: #4B5563;               /* 7.6:1 */
  --text-subtle: #5F6672;              /* 5.9:1 */
  --danger: #C62828;
  --danger-soft: #FDECEC;
  --success: #1E7B45;
  --scrim: rgba(15, 17, 21, .45);
  --shadow-pop: 0 6px 20px rgba(15, 23, 42, .10), 0 1px 3px rgba(15, 23, 42, .08);
  --shadow-float: 0 24px 60px rgba(15, 23, 42, .22);
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #0F1115;
  --surface: #15181E;
  --surface-2: #1C2027;
  --surface-3: #242932;
  --side-bg: #121419;
  --border: #262B33;
  --border-strong: #5E6672;            /* 3.1:1 on surface */
  --text: #E8EAED;
  --text-muted: #A9B0BA;               /* 8.2:1 */
  --text-subtle: #939BA7;              /* 6.2:1 */
  --danger: #F28B82;
  --danger-soft: #3A1F1F;
  --success: #6FCF97;
  --scrim: rgba(0, 0, 0, .6);
  --shadow-pop: 0 8px 24px rgba(0, 0, 0, .45);
  --shadow-float: 0 24px 60px rgba(0, 0, 0, .6);
  color-scheme: dark;
}

:root {
  /* Fallbacks until theme.js runs (and the dashboard's own identity) */
  --accent: #2563EB; --accent-ink: #1D4ED8; --accent-soft: #E9EFFD; --accent-softer: #F4F7FE;
  --on-accent: #FFFFFF; --btn-bg: #1D4ED8; --btn-fg: #FFFFFF;
  --font-body: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-heading: var(--font-body); --font-ui: var(--font-body);
  --font-mono: 'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace;
  --radius: 8px; --radius-sm: 5px; --radius-lg: 12px;
  --content-width: 760px;
  --topbar-h: 56px;
  --t-fast: 120ms ease;
  --t-panel: 200ms cubic-bezier(.2, .8, .2, 1);
  /* The HyperChat bubble takes each wiki's own accent. */
  --hc-accent: var(--accent);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-ui); font-size: 15px; line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
img { max-width: 100%; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ─── Buttons ─── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: var(--btn-bg); color: var(--btn-fg);
  font-weight: 600; font-size: 14px; cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: filter var(--t-fast), background var(--t-fast);
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .55; cursor: default; filter: none; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); filter: none; }
.btn.danger { background: var(--danger); color: #fff; }
[data-theme="dark"] .btn.danger { color: #1A0B0B; }
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn svg { width: 16px; height: 16px; }

.icon-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: var(--radius-sm); border: 0; background: transparent;
  color: var(--text-muted); cursor: pointer; transition: background var(--t-fast), color var(--t-fast);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn[aria-pressed="true"] { color: var(--accent-ink); background: var(--accent-soft); }
@media (pointer: coarse) { .icon-btn::before { content: ""; position: absolute; inset: -5px; } }

svg.ico { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ─── Forms ─── */
.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field > label, .field-label { font-size: 13px; font-weight: 600; color: var(--text); }
.field .hint { font-size: 12.5px; color: var(--text-subtle); }
.field .error { font-size: 12.5px; color: var(--danger); }
.input, .select, .textarea {
  width: 100%; min-height: 38px; padding: 8px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
.textarea { resize: vertical; min-height: 76px; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input[aria-invalid="true"] { border-color: var(--danger); }
.input-affix { display: flex; align-items: stretch; }
.input-affix .affix {
  display: flex; align-items: center; padding: 0 10px; font-size: 13px; color: var(--text-subtle);
  background: var(--surface-2); border: 1px solid var(--border-strong); border-right: 0;
  border-radius: var(--radius-sm) 0 0 var(--radius-sm); white-space: nowrap;
}
.input-affix .input { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
@media (max-width: 640px) { .input, .select, .textarea { font-size: 16px; } }

/* Segmented choice (radio group rendered as buttons) */
.choices { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.choice {
  position: relative; display: grid; gap: 2px; padding: 10px 12px; text-align: left;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); cursor: pointer;
  transition: border-color var(--t-fast), background var(--t-fast);
}
.choice:hover { border-color: var(--border-strong); }
.choice[aria-checked="true"] { border-color: var(--accent); background: var(--accent-softer); box-shadow: inset 0 0 0 1px var(--accent); }
.choice strong { font-size: 13.5px; }
.choice span { font-size: 12px; color: var(--text-subtle); }

/* Colour swatches (theme form: wizard + wiki settings) */
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  position: relative; display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%;
  border: 2px solid var(--surface); background: var(--sw); color: #fff; cursor: pointer;
  box-shadow: 0 0 0 1px var(--border);
}
.swatch svg { width: 18px; height: 18px; opacity: 0; }
.swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--text); }
.swatch[aria-checked="true"] svg { opacity: 1; }
.swatch.custom { background: conic-gradient(from 0deg, #ef4444, #f59e0b, #22c55e, #06b6d4, #6366f1, #d946ef, #ef4444); overflow: hidden; }
.swatch.custom[aria-checked="true"] { background: var(--sw); }
.swatch.custom svg { opacity: 1; filter: drop-shadow(0 0 2px rgba(0,0,0,.5)); }
.swatch.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }

/* ─── Wiki icons (js/wiki-icons.js) — fill whatever tile holds them ─── */
.wiki-icon-svg { width: 58%; height: 58%; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; display: block; }
.wiki-icon-img { width: 100%; height: 100%; object-fit: cover; border-radius: inherit; display: block; }
.wiki-icon-letter { font-weight: 700; line-height: 1; font-size: .55em; }

.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.icon-tile {
  display: grid; place-items: center; aspect-ratio: 1; padding: 0; overflow: hidden; font-size: 34px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text-muted);
  cursor: pointer; transition: border-color var(--t-fast), background var(--t-fast), color var(--t-fast);
}
.icon-tile:hover { border-color: var(--border-strong); color: var(--text); }
.icon-tile[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-ink); box-shadow: inset 0 0 0 1px var(--accent); }
.icon-tile.upload { padding: 3px; }
.icon-tile.upload .wiki-icon-img { border-radius: calc(var(--radius-sm) - 2px); }
.icon-upload { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-top: 10px; }
.icon-upload label { cursor: pointer; }
.icon-upload label:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ─── Popover menu ─── */
.menu {
  position: fixed; z-index: 70; min-width: 200px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
}
.menu button {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; border: 0;
  background: none; border-radius: var(--radius-sm); font-size: 13.5px; text-align: left; cursor: pointer; color: var(--text);
}
.menu button:hover, .menu button:focus-visible { background: var(--surface-2); outline: none; }
.menu button svg { width: 16px; height: 16px; color: var(--text-muted); }
.menu button.danger { color: var(--danger); }
.menu button.danger svg { color: var(--danger); }
.menu button:disabled { opacity: .45; cursor: default; }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 6px 4px; }

/* ─── Modal ─── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: var(--scrim);
  display: flex; align-items: flex-start; justify-content: center; padding: 8vh 16px 16px;
  animation: fade-in var(--t-panel);
}
.modal {
  width: 100%; max-width: 480px; max-height: 84vh; overflow: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-float);
  animation: pop-in var(--t-panel);
}
.modal.wide { max-width: 640px; }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 18px 20px 6px; }
.modal-head h2 { margin: 0; font-size: 17px; font-weight: 650; flex: 1; }
.modal-body { padding: 10px 20px 4px; }
.modal-body > p { margin: 0 0 14px; color: var(--text-muted); font-size: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px 18px; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(6px) scale(.985); } }

/* ─── Toast ─── */
.toasts { position: fixed; z-index: 80; left: 50%; bottom: 24px; transform: translateX(-50%); display: grid; gap: 8px; width: max-content; max-width: calc(100vw - 32px); }
.toast {
  padding: 10px 16px; border-radius: var(--radius); font-size: 14px; font-weight: 500;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow-pop); animation: pop-in var(--t-panel);
}
.toast.error { background: var(--danger); color: #fff; }
[data-theme="dark"] .toast.error { color: #1A0B0B; }

/* ─── Misc ─── */
.chip {
  display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--text-muted); white-space: nowrap;
}
.chip.accent { background: var(--accent-soft); color: var(--accent-ink); }
.chip svg { width: 12px; height: 12px; }
.kbd {
  display: inline-block; min-width: 20px; padding: 1px 5px; border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 4px; font: 500 11px/1.4 var(--font-mono); color: var(--text-subtle); text-align: center; background: var(--surface);
}
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.4s ease infinite; border-radius: var(--radius-sm); }
@keyframes shimmer { to { background-position: -200% 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
