/*
 * stormblessed: the shared look of every app on stormblessed.fr.
 *
 * Dark, monospace, square. One green accent; amber for warnings, red for
 * danger. Everything is in cascade layers, so an app's own unlayered rules
 * always win without fighting specificity.
 *
 * The style guide (../index.html) shows every component and its states.
 */

@layer tokens, base, components;

/* ── Tokens ────────────────────────────────────────────────── */

@layer tokens {
  :root {
    color-scheme: dark;

    /* Surfaces, darkest to lightest. */
    --bg: #1b1c1b;
    --surface: #1f201f;
    --band: #242624;
    --hover: #2a2d2a;
    --selected: #233023;
    --border: #363936;
    --scrollbar: #3f443f;

    --text: #dcdccc;
    --text-muted: #a3a89e;
    --text-dim: #6f756c;
    --accent: #6fd36f;
    --accent-dim: #3d7a3d;
    --warn: #e0b45a;
    --danger: #e57373;

    --font: "JetBrains Mono", "IBM Plex Mono", "Fira Code", ui-monospace, "SF Mono", Menlo,
      Consolas, monospace;
    --text-sm: 12px;
    --text-md: 14px;
    --text-lg: 16px;
    --text-xl: 22px;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;

    /* Height of buttons and single-line fields. */
    --control: 26px;
    --sidebar-width: 240px;

    --duration: 200ms;
    --shadow: 0 6px 16px rgb(0 0 0 / 0.5);
  }
}

/* ── Base ──────────────────────────────────────────────────── */

@layer base {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
    border-radius: 0;
  }

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

  /* Components set display, which would otherwise beat the hidden
     attribute. Important in a layer still outranks any normal rule. */
  [hidden] {
    display: none !important;
  }

  body {
    background: var(--bg);
    color: var(--text);
    font: 400 var(--text-md) / 1.5 var(--font);
    -webkit-font-smoothing: antialiased;
  }

  * {
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar) transparent;
  }

  h1,
  h2,
  h3 {
    margin: 0;
    font-size: inherit;
  }

  a {
    color: var(--accent);
    text-underline-offset: 3px;
  }

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

  button {
    cursor: pointer;
  }

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

  ::selection {
    background: var(--accent-dim);
    color: var(--text);
  }

  code,
  pre {
    font: inherit;
  }

  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0s !important;
      transition-duration: 0s !important;
    }
  }
}

/* ── Components ────────────────────────────────────────────── */

@layer components {
  /* Icons come from icons.svg: <svg class="icon"><use href="icons.svg#check"/></svg>.
     Strokes take currentColor. */
  .icon {
    display: block;
    width: 16px;
    height: 16px;
    flex: none;
  }

  .icon--sm {
    width: 12px;
    height: 12px;
  }

  /* Shell: a header across the top, nav on the left, main on the right.
     The nav hides on narrow screens. */
  .shell {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
  }

  .shell__header {
    grid-column: 1 / -1;
    padding: 20px var(--space-5) var(--space-4);
  }

  .shell__nav {
    padding: var(--space-2) var(--space-3) var(--space-5) var(--space-5);
    overflow-y: auto;
  }

  .shell__main {
    min-width: 0;
    overflow: auto;
  }

  .shell__status {
    grid-column: 1 / -1;
    margin: 0;
    padding: var(--space-5);
    color: var(--text-dim);
  }

  .shell__status[role="alert"] {
    color: var(--danger);
  }

  @media (max-width: 720px) {
    .shell {
      grid-template-columns: minmax(0, 1fr);
    }

    .shell__nav {
      display: none;
    }
  }

  /* The page title, underlined in the accent. */
  .title {
    display: inline-block;
    margin: 0;
    padding-bottom: 2px;
    border-bottom: 3px solid var(--accent);
    font-size: var(--text-xl);
    font-weight: 700;
    letter-spacing: 0.02em;
    color: var(--text);
  }

  /* A section heading on a lighter band. */
  .band {
    margin: 0;
    padding: 6px 10px;
    overflow: hidden;
    background: var(--band);
    font-size: var(--text-md);
    font-weight: 700;
    color: var(--accent);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .band--lg {
    padding: var(--space-2) 14px;
    font-size: var(--text-lg);
  }

  /* A bordered surface: a column, a card. */
  .panel {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    border: 1px solid var(--border);
    background: var(--surface);
  }

  /* Nav list: rows with a label and an optional trailing count. */
  .nav {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .nav__item {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    height: 34px;
    padding: 0 10px;
    color: inherit;
    text-decoration: none;
  }

  .nav__item:hover,
  .nav__item--current {
    background: var(--hover);
  }

  .nav__item--current {
    box-shadow: inset 2px 0 var(--accent);
  }

  .nav__label {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .nav__count {
    color: var(--text-dim);
  }

  /* A list row that lights up under the pointer or keyboard focus. */
  .row {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: 7px var(--space-2) 7px var(--space-3);
    border-left: 2px solid transparent;
  }

  .row:hover,
  .row--focused {
    background: var(--hover);
  }

  .row--focused {
    border-left-color: var(--accent-dim);
  }

  .row--selected {
    background: var(--selected);
  }

  /* Buttons: bordered, square, quiet until hovered. */
  .btn {
    display: inline-grid;
    grid-auto-flow: column;
    place-items: center;
    gap: 6px;
    height: var(--control);
    padding: 0 var(--space-2);
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text-muted);
  }

  .btn:hover:not(:disabled) {
    border-color: var(--accent);
    color: var(--accent);
  }

  .btn:disabled {
    opacity: 0.35;
    cursor: default;
  }

  .btn--icon {
    width: var(--control);
    padding: 0;
  }

  /* The one thing to do: bold accent text. */
  .btn--primary {
    color: var(--accent);
    font-weight: 700;
  }

  /* No border until hovered: toolbars, toasts, rows. */
  .btn--ghost {
    border-color: transparent;
    background: none;
  }

  .btn--danger:hover:not(:disabled) {
    border-color: var(--danger);
    color: var(--danger);
  }

  /* Text fields, dates, selects and textareas. */
  .input {
    height: var(--control);
    min-width: 0;
    padding: 0 6px;
    border: 1px solid var(--border);
    outline: 0;
    background: var(--bg);
    color: var(--text);
    color-scheme: dark;
  }

  .input:focus {
    border-color: var(--accent-dim);
  }

  .input::placeholder {
    color: var(--text-dim);
  }

  textarea.input {
    width: 100%;
    height: auto;
    min-height: 3.5em;
    padding: 6px var(--space-2);
    resize: vertical;
  }

  select.input {
    padding-right: 22px;
    appearance: none;
    /* A small chevron, drawn so it needs no image. */
    background:
      linear-gradient(45deg, transparent 50%, var(--text-muted) 50%) no-repeat
        calc(100% - 11px) 50% / 5px 5px,
      linear-gradient(-45deg, transparent 50%, var(--text-muted) 50%) no-repeat
        calc(100% - 6px) 50% / 5px 5px,
      var(--bg);
    cursor: pointer;
  }

  /* Underline only: editing text in place. */
  .input--line {
    height: auto;
    padding: 0 0 1px;
    border: 0;
    border-bottom: 1px solid var(--accent);
    background: none;
  }

  /* No chrome at all: a field that reads as text. */
  .input--bare {
    height: auto;
    padding: 0;
    border: 0;
    background: none;
  }

  /* A label next to its control. */
  .field {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
  }

  .field__label {
    color: var(--text-dim);
  }

  /* Label above a full-width control: forms with several fields. */
  .field--stack {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-1);
  }

  /* Square check. Either a <button class="check"> or a visually hidden
     <input class="check-input" type="checkbox"> followed by <span class="check">. */
  .check {
    display: grid;
    place-items: center;
    width: 16px;
    height: 16px;
    flex: none;
    padding: 0;
    border: 1px solid var(--text-muted);
    background: none;
    color: var(--bg);
  }

  .check .icon {
    width: 12px;
    height: 12px;
    opacity: 0;
  }

  button.check {
    color: var(--text-muted);
  }

  button.check:hover {
    border-color: var(--accent);
    color: var(--accent);
  }

  button.check:hover .icon {
    opacity: 1;
  }

  .check--on,
  .check-input:checked + .check {
    border-color: var(--accent);
    background: var(--accent);
    color: var(--bg);
  }

  .check--on .icon,
  .check-input:checked + .check .icon {
    opacity: 1;
  }

  .check-input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }

  .check-input:focus-visible + .check {
    outline: 1px solid var(--accent);
    outline-offset: 2px;
  }

  /* A small bordered tag: a date, a count, a state. */
  .badge {
    display: inline-block;
    padding: 0 6px;
    border: 1px solid var(--border);
    font-size: var(--text-sm);
    color: var(--text-muted);
    white-space: nowrap;
  }

  .badge--accent {
    border-color: var(--accent-dim);
    color: var(--accent);
  }

  .badge--warn {
    border-color: var(--warn);
    color: var(--warn);
  }

  .badge--danger {
    border-color: var(--danger);
    color: var(--danger);
  }

  /* A collapsible section's header: chevron, label, full width. */
  .disclosure {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    padding: var(--space-2) 14px;
    border: 0;
    border-top: 1px solid var(--border);
    background: none;
    color: var(--text-muted);
    text-align: left;
  }

  .disclosure:hover {
    background: var(--hover);
    color: var(--text);
  }

  .disclosure .icon {
    transform: rotate(-90deg);
  }

  .disclosure[aria-expanded="true"] .icon {
    transform: none;
  }

  /* A framed square showing a small image: a favicon, an avatar. Empty, it
     shows a dashed outline. */
  .thumb {
    display: grid;
    place-items: center;
    width: var(--control);
    height: var(--control);
    flex: none;
    padding: 0;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text-dim);
  }

  .thumb img {
    display: block;
    width: 16px;
    height: 16px;
    object-fit: contain;
  }

  .thumb--lg {
    width: 48px;
    height: 48px;
  }

  .thumb--lg img {
    width: 32px;
    height: 32px;
  }

  .thumb--empty {
    border-style: dashed;
  }

  button.thumb:hover {
    border-color: var(--accent);
    color: var(--accent);
  }

  /* Somewhere to drop a file, or click to pick one. Add --over while a file
     is dragged over it. */
  .dropzone {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: 64px;
    padding: var(--space-3);
    border: 1px dashed var(--border);
    color: var(--text-dim);
    text-align: center;
    cursor: pointer;
  }

  .dropzone:hover,
  .dropzone--over {
    border-color: var(--accent);
    color: var(--accent);
  }

  .dropzone--over {
    background: var(--selected);
  }

  /* Toast: one at a time, bottom left. */
  .toast-slot {
    position: fixed;
    bottom: var(--space-5);
    left: var(--space-5);
    z-index: 10;
  }

  .toast {
    display: flex;
    align-items: center;
    gap: var(--space-5);
    min-width: 288px;
    max-width: min(560px, calc(100vw - 48px));
    padding: var(--space-1) var(--space-1) var(--space-1) 14px;
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    background: var(--band);
    color: var(--text);
    box-shadow: var(--shadow);
  }

  .toast__message {
    flex: 1;
    padding: 6px 0;
  }

  .toast--error {
    border-left-color: var(--danger);
    color: var(--danger);
  }

  /* Text helpers. */
  .muted {
    color: var(--text-muted);
  }

  .dim {
    color: var(--text-dim);
  }

  .spacer {
    flex: 1;
  }
}
