/* ------------------------------------------------------------------------------------------------
   Design tokens. Dark is the default; theme.js sets data-theme="light" for the light variant.
   Change the palette here and everything follows. No build step: plain CSS custom properties.
   ------------------------------------------------------------------------------------------------ */
:root {
  --font: "Aptos", "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
  --radius: 14px;
  --radius-lg: 20px;

  color-scheme: dark;
  --bg: #0a0f1f;
  --glow-1: rgba(122, 77, 255, 0.2);
  --glow-2: rgba(59, 108, 255, 0.18);
  --surface: #101733;
  --surface-hover: #172046;
  --field: #0b1129;
  --border: rgba(148, 163, 255, 0.16);
  --border-strong: rgba(148, 163, 255, 0.3);
  --text: #eef1fb;
  --text-soft: #c5cde6;
  --text-muted: #96a2c6;
  --text-faint: #7280a8;
  --accent: #5b8cff;
  --accent-text: #a3bcff;
  --accent-soft: rgba(91, 140, 255, 0.14);
  --link: #93b2ff;
  --ring: rgba(91, 140, 255, 0.38);
  --grad: linear-gradient(135deg, #3b6cff 0%, #7a4dff 100%);
  --chip-bg: rgba(255, 255, 255, 0.05);
  --header-bg: rgba(10, 15, 31, 0.72);
  --shadow-lg: 0 18px 40px -14px rgba(0, 0, 0, 0.6);
  --ok: #8adbb8;
  --ok-soft: rgba(138, 219, 184, 0.12);
  --bad: #f2a3b6;
  --bad-soft: rgba(242, 163, 182, 0.12);

  --icon-play: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='white' d='M8 5v14l11-7z'/%3E%3C/svg%3E");
  --icon-bars: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='white' d='M3 10h2v4H3zm4-4h2v12H7zm4-3h2v18h-2zm4 4h2v10h-2zm4 3h2v4h-2z'/%3E%3C/svg%3E");
  --icon-sun: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='12' cy='12' r='4.5'/%3E%3Cpath d='M12 1.5v3M12 19.5v3M1.5 12h3M19.5 12h3M4.6 4.6l2.1 2.1M17.3 17.3l2.1 2.1M4.6 19.4l2.1-2.1M17.3 6.7l2.1-2.1'/%3E%3C/svg%3E");
  --icon-moon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z'/%3E%3C/svg%3E");
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6fc;
  --glow-1: rgba(122, 77, 255, 0.1);
  --glow-2: rgba(59, 108, 255, 0.1);
  --surface: #ffffff;
  --surface-hover: #f0f3fb;
  --field: #ffffff;
  --border: rgba(30, 41, 99, 0.12);
  --border-strong: rgba(30, 41, 99, 0.24);
  --text: #101736;
  --text-soft: #2c3560;
  --text-muted: #566190;
  --text-faint: #69739a;
  --accent: #2f5fe0;
  --accent-text: #2447b8;
  --accent-soft: rgba(47, 95, 224, 0.1);
  --link: #2447b8;
  --ring: rgba(47, 95, 224, 0.3);
  --chip-bg: rgba(30, 41, 99, 0.05);
  --header-bg: rgba(244, 246, 252, 0.82);
  --shadow-lg: 0 18px 40px -18px rgba(30, 41, 99, 0.3);
  --ok: #17724f;
  --ok-soft: rgba(23, 114, 79, 0.1);
  --bad: #a8284a;
  --bad-soft: rgba(168, 40, 74, 0.1);
}

/* ------------------------------------------------------------------------------------------------
   Base
   ------------------------------------------------------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(1100px 520px at 85% -8%, var(--glow-1), transparent 62%),
    radial-gradient(900px 480px at -8% 0%, var(--glow-2), transparent 58%);
  background-repeat: no-repeat;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.2; letter-spacing: -0.02em; }
h1 { font-size: clamp(1.75rem, 1.2rem + 2vw, 2.5rem); font-weight: 700; margin: 0 0 0.5em; }
h2 { font-size: 1.25rem; font-weight: 650; }
a { color: var(--link); }
p { margin: 0 0 1em; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

main {
  max-width: 1120px;
  margin: 0 auto;
  padding: clamp(20px, 4vw, 48px) clamp(16px, 4vw, 40px) 80px;
}

.meta { color: var(--text-muted); font-size: 0.92rem; margin: 0 0 1em; }
#status { color: var(--text-muted); min-height: 1.5em; margin: 0 0 1em; }
#status:empty { display: none; }
#status.empty { text-align: center; padding: 56px 0; font-size: 1.05rem; }

/* ------------------------------------------------------------------------------------------------
   Header
   ------------------------------------------------------------------------------------------------ */
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(16px, 4vw, 40px);
  background: var(--header-bg);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 650;
  letter-spacing: -0.01em;
  text-decoration: none;
  white-space: nowrap;
}
.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: var(--grad);
  box-shadow: 0 0 20px rgba(122, 77, 255, 0.55);
}
.site-header nav { display: flex; align-items: center; gap: 6px; }
.site-header nav a {
  color: var(--text-muted);
  text-decoration: none;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.92rem;
  white-space: nowrap;
}
.site-header nav a:hover { color: var(--text); background: var(--surface-hover); }
#adminLink { color: var(--text); border: 1px solid var(--border-strong); }
#who { color: var(--text-muted); font-size: 0.88rem; padding: 0 8px; }

/* ------------------------------------------------------------------------------------------------
   Controls
   ------------------------------------------------------------------------------------------------ */
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  background: var(--field);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
textarea { min-height: 120px; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--text-faint); }
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}
select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 40px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 21px) 52%, calc(100% - 16px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
input[type="file"] { padding: 8px 12px; }
progress { width: 100%; height: 8px; accent-color: var(--accent); }

button {
  min-height: 40px;
  padding: 8px 18px;
  cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  transition: background 0.15s, border-color 0.15s, transform 0.15s, filter 0.15s;
}
button:hover { background: var(--surface-hover); border-color: var(--accent); }
button:active { transform: translateY(1px); }
button[type="submit"], button.primary, #pageSave {
  background: var(--grad);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
button[type="submit"]:hover, button.primary:hover, #pageSave:hover { filter: brightness(1.12); border-color: transparent; }
button:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }

.icon-btn { width: 40px; padding: 0; display: inline-grid; place-items: center; border-radius: 50%; }
.icon-btn::before {
  content: "";
  width: 18px;
  height: 18px;
  background: currentColor;
  -webkit-mask: var(--i) center / contain no-repeat;
  mask: var(--i) center / contain no-repeat;
}
:root[data-theme="dark"] .icon-btn { --i: var(--icon-sun); }
:root[data-theme="light"] .icon-btn { --i: var(--icon-moon); }

.back { margin-bottom: 20px; }

/* ------------------------------------------------------------------------------------------------
   Access code screen
   ------------------------------------------------------------------------------------------------ */
.access { min-height: 56vh; display: grid; place-items: center; }
.access-card {
  width: min(480px, 100%);
  padding: clamp(24px, 5vw, 40px);
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow-lg);
}
.access-card h1 {
  font-size: 1.75rem;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.access-card .form { margin: 24px 0 0; padding: 0; background: none; border: 0; max-width: none; }
#code { text-align: center; font-family: var(--mono); font-size: 0.95rem; letter-spacing: 0.06em; }
.access-card .staff-link { margin: 20px 0 0; font-size: 0.9rem; }
.access-card .staff-link a { color: var(--text-muted); }

/* ------------------------------------------------------------------------------------------------
   Browse: customer page, filters, cards
   ------------------------------------------------------------------------------------------------ */
.filters { display: flex; flex-wrap: wrap; gap: 12px; margin: 8px 0 8px; }
.filters input { flex: 2 1 260px; width: auto; }
.filters select { flex: 1 1 170px; width: auto; }
.section-heading {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 36px 0 16px;
  font-size: 1.1rem;
  color: var(--text-soft);
}
.section-heading::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 20px; margin-top: 16px; }
.section-heading + .grid { margin-top: 0; }

.card {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: inherit;
  text-decoration: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  transition: transform 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
}
.card:hover { transform: translateY(-3px); border-color: var(--accent); box-shadow: var(--shadow-lg); }
.card:focus-visible { outline-offset: 2px; }

/* Cards have no thumbnails, so each gets a gradient (hue varies per file, set in app.js) and a type icon. */
.thumb {
  --h: 235;
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 16 / 9;
  background: linear-gradient(135deg, hsl(var(--h) 85% 50%), hsl(calc(var(--h) + 32) 70% 36%));
}
.thumb::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.3), transparent 55%);
}
.thumb::after {
  content: "";
  position: relative;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2) var(--icon) center / 26px no-repeat;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  transition: transform 0.18s ease;
}
.card:hover .thumb::after { transform: scale(1.08); }
.thumb-video { --icon: var(--icon-play); }
/* Audio cards show the file's waveform (app/waveform.js) on the same dark panel as the player, not the colour gradient. */
.thumb-audio { --icon: var(--icon-bars); background: linear-gradient(160deg, #121836, #1b1a42); }
.thumb-audio::before { background: radial-gradient(circle at 15% 10%, rgba(122, 77, 255, 0.18), transparent 60%); }
.thumb-audio::after { display: none; }
.thumb-wave { position: absolute; left: 16px; bottom: 20px; width: calc(100% - 32px); height: 52%; }
.badge {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 1;
  padding: 3px 10px;
  font-size: 0.7rem;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: #fff;
  background: rgba(8, 12, 30, 0.55);
  border-radius: 999px;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.card-body { display: flex; flex-direction: column; gap: 10px; padding: 16px 18px 18px; }
.card h2 { margin: 0; font-size: 1.05rem; line-height: 1.3; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 2px 10px;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: var(--chip-bg);
  border: 1px solid var(--border);
  border-radius: 999px;
}
.chip-org { color: var(--accent-text); background: var(--accent-soft); border-color: transparent; }
.chip-ok { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.chip-bad { color: var(--bad); background: var(--bad-soft); border-color: transparent; }

/* ------------------------------------------------------------------------------------------------
   Markdown (customer page and the admin preview)
   ------------------------------------------------------------------------------------------------ */
.markdown {
  position: relative;
  margin: 0 0 32px;
  padding: clamp(20px, 3.5vw, 40px);
  overflow: hidden;
  overflow-wrap: anywhere;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
#orgPage::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; background: var(--grad); }
/* A soft glow in the empty right-hand side, since the text keeps to a readable width. */
#orgPage::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(520px 320px at 100% 0%, rgba(122, 77, 255, 0.2), transparent 70%);
}
#orgPage > * { position: relative; z-index: 1; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown > * { max-width: 72ch; }
.markdown h1 { font-size: clamp(1.75rem, 1.2rem + 1.8vw, 2.4rem); }
.markdown h2 { font-size: 1.4rem; margin: 1.7em 0 0.5em; }
.markdown h3 { font-size: 1.15rem; margin: 1.5em 0 0.4em; }
.markdown p, .markdown li { color: var(--text-soft); }
.markdown li { margin: 0.25em 0; }
.markdown img, .markdown video { max-width: 100%; height: auto; border-radius: 12px; }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 2em 0; }
.markdown blockquote {
  margin: 1.2em 0;
  padding: 0.6em 1.1em;
  color: var(--text-soft);
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  border-radius: 0 12px 12px 0;
}
.markdown blockquote p { margin: 0.4em 0; }
.markdown code { padding: 2px 6px; font-family: var(--mono); font-size: 0.88em; background: var(--chip-bg); border: 1px solid var(--border); border-radius: 6px; }
.markdown pre { padding: 14px 16px; overflow-x: auto; background: var(--field); border: 1px solid var(--border); border-radius: 12px; }
.markdown pre code { padding: 0; background: none; border: 0; }
.markdown table { border-collapse: collapse; margin: 1.2em 0; display: block; overflow-x: auto; }
.markdown th, .markdown td { padding: 8px 14px; text-align: left; border: 1px solid var(--border); }
.markdown th { background: var(--accent-soft); color: var(--text); }

/* ------------------------------------------------------------------------------------------------
   Player
   ------------------------------------------------------------------------------------------------ */
.stage video {
  display: block;
  width: 100%;
  max-height: 72vh;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
/* A dark panel in both themes so the white "played" part of the waveform always shows. */
.audio-stage {
  display: grid;
  gap: 20px;
  padding: clamp(20px, 4vw, 32px);
  background: linear-gradient(160deg, #121836, #1b1a42);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  color-scheme: dark;
}
.waveform { display: block; width: 100%; height: 72px; cursor: pointer; touch-action: none; }
.audio-stage audio { width: 100%; }
#player h1 { margin: 28px 0 12px; }
#player .chips { margin-bottom: 20px; }
#pDesc { max-width: 72ch; color: var(--text-soft); white-space: pre-line; }

/* ------------------------------------------------------------------------------------------------
   Admin
   ------------------------------------------------------------------------------------------------ */
/* Section titles only: direct children, so the <h1> inside the rendered preview keeps its markdown styling. */
#adminBody > h1, #pageEditor > h1, #keysPanel > h1 { font-size: 1.5rem; margin: 48px 0 16px; }
#adminBody > h1:first-child { margin-top: 0; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 0.875rem; color: var(--text-muted); }
small { color: var(--text-muted); }
.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 560px;
  margin: 0 0 20px;
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
#orgList, #staffList { display: grid; gap: 10px; margin: 0 0 20px; padding: 0; list-style: none; }
#staffList .meta { margin: 0; font-size: 0.85rem; }
#staffForm label { flex: 1 1 280px; }
#orgList li, #staffList li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
/* Access keys panel */
.key-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin: 0 0 16px; }
.key-form label { min-width: 150px; }
#keyList { display: grid; gap: 10px; margin: 0 0 20px; padding: 0; list-style: none; }
.key-row {
  display: grid;
  gap: 8px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.key-row.is-expired { opacity: 0.7; }
.key-main { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; }
.key-code { padding: 4px 10px; font-family: var(--mono); font-size: 0.95rem; letter-spacing: 0.04em; overflow-wrap: anywhere; background: var(--field); border: 1px solid var(--border); border-radius: 8px; }
.key-hidden { color: var(--text-muted); font-style: italic; }
.key-meta { color: var(--text-muted); font-size: 0.85rem; }
.key-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.key-empty { color: var(--text-muted); }
.editor { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 12px; }
.editor-pane { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.editor-pane textarea { flex: 1; min-height: 440px; font-family: var(--mono); font-size: 0.9rem; line-height: 1.5; }
.editor-pane .markdown { flex: 1; min-height: 440px; margin-bottom: 0; padding: 24px; }
.editor-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }

/* Upload form: title, description and file span the width; the pickers sit in two columns. */
#videoForm { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; max-width: 760px; }
#videoForm > :not(label) { grid-column: 1 / -1; }
#videoForm > label { min-width: 0; }
#orgForm { max-width: 760px; flex-direction: row; flex-wrap: wrap; }
#orgForm input { flex: 1 1 200px; width: auto; }

/* ------------------------------------------------------------------------------------------------
   Responsive and motion
   ------------------------------------------------------------------------------------------------ */
@media (max-width: 800px) {
  .editor { grid-template-columns: 1fr; }
  .editor-pane textarea, .editor-pane .markdown { min-height: 260px; }
}
@media (max-width: 560px) {
  #who { display: none; }
  .grid { grid-template-columns: 1fr; }
  .site-header { gap: 8px; }
  .site-header nav { gap: 2px; }
  .site-header nav a { padding: 6px 10px; }
  .brand { font-size: 0.95rem; gap: 8px; }
  #videoForm { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
  .card:hover { transform: none; }
}
