/* stellang chat — a small design system in three layers:
   1. primitives: raw scales (color, space, type, radius, shadow), never used by components directly
   2. semantic tokens: what a color is *for* (surface, text, border, accent); themes only remap these
   3. components: built only from semantic tokens */
@layer primitives, tokens, base, components, utilities;

@layer primitives {
  :root {
    /* warm stone neutrals */
    --stone-0: #ffffff;
    --stone-50: #fafaf9;
    --stone-100: #f5f5f4;
    --stone-150: #efeeec;
    --stone-200: #e7e5e4;
    --stone-300: #d6d3d1;
    --stone-400: #a8a29e;
    --stone-500: #78716c;
    --stone-600: #57534e;
    --stone-700: #44403c;
    --stone-800: #292524;
    --stone-850: #201d1b;
    --stone-900: #1c1917;
    --stone-950: #12100f;

    /* one accent: a deep green; one signal: amber for mentions */
    --green-50: #ecf8f1;
    --green-100: #d3f0df;
    --green-400: #3fae73;
    --green-500: #1f8f55;
    --green-600: #157545;
    --green-700: #115c37;
    --amber-100: #fdf0c7;
    --amber-400: #e9a817;
    --amber-600: #a86b00;
    --red-500: #d1453b;

    /* avatar palette, picked to stay readable with white text */
    --hue-0: #c2410c;
    --hue-1: #b45309;
    --hue-2: #15803d;
    --hue-3: #0f766e;
    --hue-4: #0369a1;
    --hue-5: #4338ca;
    --hue-6: #a21caf;
    --hue-7: #be123c;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;

    --text-xs: 12px;
    --text-sm: 13px;
    --text-md: 15px;
    --text-lg: 17px;
    --text-xl: 22px;
    --text-2xl: 28px;

    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-full: 999px;

    --font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;

    --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);
    --dur-fast: 120ms;
    --dur-med: 220ms;
  }
}

@layer tokens {
  :root {
    color-scheme: light;
    --surface-app: var(--stone-0);
    --surface-sidebar: var(--stone-100);
    --surface-raised: var(--stone-0);
    --surface-sunken: var(--stone-100);
    --surface-hover: var(--stone-150);
    --surface-selected: var(--stone-200);
    --surface-inverse: var(--stone-900);

    --text-strong: var(--stone-900);
    --text-default: var(--stone-800);
    --text-muted: var(--stone-500);
    --text-faint: var(--stone-400);
    --text-inverse: var(--stone-50);

    --border-subtle: var(--stone-200);
    --border-default: var(--stone-300);
    --border-focus: var(--green-500);

    --accent: var(--green-500);
    --accent-hover: var(--green-600);
    --accent-text: var(--green-700);
    --accent-soft: var(--green-50);
    --accent-on: #ffffff;

    --mention-bg: var(--amber-100);
    --mention-bar: var(--amber-400);
    --mention-text: var(--amber-600);
    --danger: var(--red-500);
    --online: var(--green-400);

    --shadow-sm: 0 1px 2px rgb(28 25 23 / 0.06);
    --shadow-md: 0 2px 6px rgb(28 25 23 / 0.06), 0 8px 24px rgb(28 25 23 / 0.08);
    --shadow-lg: 0 24px 64px rgb(28 25 23 / 0.14);
  }

  @media (prefers-color-scheme: dark) {
    :root {
      color-scheme: dark;
      --surface-app: var(--stone-900);
      --surface-sidebar: var(--stone-950);
      --surface-raised: var(--stone-850);
      --surface-sunken: var(--stone-950);
      --surface-hover: var(--stone-800);
      --surface-selected: var(--stone-700);
      --surface-inverse: var(--stone-100);

      --text-strong: var(--stone-50);
      --text-default: var(--stone-200);
      --text-muted: var(--stone-400);
      --text-faint: var(--stone-500);
      --text-inverse: var(--stone-900);

      --border-subtle: var(--stone-800);
      --border-default: var(--stone-700);
      --border-focus: var(--green-400);

      --accent: var(--green-500);
      --accent-hover: var(--green-400);
      --accent-text: var(--green-400);
      --accent-soft: rgb(31 143 85 / 0.16);

      --mention-bg: rgb(233 168 23 / 0.12);
      --mention-text: var(--amber-400);

      --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.3);
      --shadow-md: 0 2px 6px rgb(0 0 0 / 0.3), 0 8px 24px rgb(0 0 0 / 0.35);
      --shadow-lg: 0 24px 64px rgb(0 0 0 / 0.5);
    }
  }
}

@layer base {
  *, *::before, *::after { box-sizing: border-box; }
  html, body { height: 100%; }
  body {
    margin: 0;
    background: var(--surface-app);
    color: var(--text-default);
    font: 400 var(--text-md) / 1.5 var(--font-sans);
    font-feature-settings: "cv11", "ss01";
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }
  #app { height: 100%; }
  h1, h2, p { margin: 0; }
  a { color: var(--accent-text); text-underline-offset: 2px; }
  button, input { font: inherit; color: inherit; }
  button { cursor: pointer; }
  fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
  :focus-visible { outline: 2px solid var(--border-focus); outline-offset: 2px; }
  code {
    font-family: var(--font-mono);
    font-size: 0.86em;
    padding: 1px 5px;
    border-radius: var(--radius-sm);
    background: var(--surface-sunken);
    border: 1px solid var(--border-subtle);
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation: none !important; }
  }
}

@layer components {
  /* ---- primitives: buttons, inputs, badges, avatars ---- */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    height: 36px;
    padding: 0 var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-default);
    background: var(--surface-raised);
    font-weight: 600;
    font-size: var(--text-sm);
    transition: background var(--dur-fast), border-color var(--dur-fast), opacity var(--dur-fast);
  }
  .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-on); }
  .btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
  .btn:disabled { opacity: 0.45; cursor: default; }
  .btn.block { width: 100%; height: 42px; font-size: var(--text-md); }

  .icon-btn {
    display: inline-grid;
    place-items: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-size: 16px;
    line-height: 1;
  }
  .icon-btn:hover { background: var(--surface-hover); color: var(--text-strong); }

  .input {
    width: 100%;
    height: 40px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-default);
    background: var(--surface-raised);
    color: var(--text-strong);
    transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
  }
  .input::placeholder { color: var(--text-faint); }
  .input:focus { outline: none; border-color: var(--border-focus); box-shadow: 0 0 0 3px var(--accent-soft); }
  .input.sm { height: 32px; font-size: var(--text-sm); }

  .badge {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: var(--radius-full);
    background: var(--accent);
    color: var(--accent-on);
    font-size: var(--text-xs);
    font-weight: 700;
    line-height: 20px;
    text-align: center;
    font-variant-numeric: tabular-nums;
  }
  .mention-badge { background: var(--mention-bar); color: var(--stone-900); }

  /* shows only the first letter of the name it holds */
  .avatar {
    --size: 36px;
    display: inline-block;
    flex: none;
    width: var(--size);
    height: var(--size);
    border-radius: var(--radius-md);
    overflow: hidden;
    color: #fff;
    font-size: 0;
    line-height: var(--size);
    text-align: center;
    white-space: nowrap;
    user-select: none;
  }
  .avatar::first-letter { font-size: calc(var(--size) * 0.44); font-weight: 600; text-transform: uppercase; }
  .avatar.sm { --size: 24px; border-radius: var(--radius-sm); }
  .avatar.lg { --size: 44px; border-radius: var(--radius-lg); }
  .c-0 { background: var(--hue-0); } .c-1 { background: var(--hue-1); }
  .c-2 { background: var(--hue-2); } .c-3 { background: var(--hue-3); }
  .c-4 { background: var(--hue-4); } .c-5 { background: var(--hue-5); }
  .c-6 { background: var(--hue-6); } .c-7 { background: var(--hue-7); }

  .brand { display: flex; align-items: center; gap: var(--space-2); font-weight: 700; font-size: var(--text-md); color: var(--text-strong); letter-spacing: -0.01em; }
  .brand-mark { display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: var(--radius-sm); background: var(--surface-inverse); color: var(--text-inverse); font-size: 15px; }

  .section-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-4) var(--space-2) var(--space-1) var(--space-3);
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }
  .muted { color: var(--text-muted); }

  /* ---- join screen ---- */
  .join { min-height: 100%; display: grid; place-items: center; padding: var(--space-6) var(--space-4); background: var(--surface-sidebar); }
  .join-card {
    width: min(420px, 100%);
    display: grid;
    gap: var(--space-5);
    padding: var(--space-8);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    background: var(--surface-raised);
    box-shadow: var(--shadow-lg);
  }
  .join-card h1 { font-size: var(--text-2xl); line-height: 1.15; letter-spacing: -0.025em; color: var(--text-strong); margin-top: var(--space-2); }
  .join-card > .muted { margin-top: calc(var(--space-3) * -1); }
  .field { display: grid; gap: var(--space-2); }
  .label { font-size: var(--text-sm); font-weight: 600; color: var(--text-strong); padding: 0; }
  .swatches { display: flex; flex-wrap: wrap; gap: var(--space-2); }
  .swatch { width: 30px; height: 30px; border-radius: var(--radius-full); border: 0; box-shadow: inset 0 0 0 2px rgb(255 255 255 / 0.15); transition: transform var(--dur-fast) var(--ease-out); }
  .swatch:hover { transform: scale(1.08); }
  .swatch.on { box-shadow: 0 0 0 2px var(--surface-raised), 0 0 0 4px var(--text-strong); }
  .join-preview { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3); border-radius: var(--radius-lg); background: var(--surface-sunken); font-weight: 600; color: var(--text-strong); }
  .error { color: var(--danger); font-size: var(--text-sm); }

  /* ---- app shell ---- */
  .shell { height: 100%; display: grid; grid-template-columns: 264px minmax(0, 1fr) 232px; }
  .sidebar { display: flex; flex-direction: column; min-height: 0; padding: var(--space-4) var(--space-2) var(--space-2); background: var(--surface-sidebar); border-right: 1px solid var(--border-subtle); }
  .sidebar > .brand { padding: var(--space-1) var(--space-3) var(--space-2); }
  .channels { flex: 1; overflow-y: auto; }
  .new-channel { padding: var(--space-1) var(--space-2) var(--space-2); }
  .channel {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    height: 34px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    text-decoration: none;
    font-weight: 500;
    transition: background var(--dur-fast), color var(--dur-fast);
  }
  .channel:hover { background: var(--surface-hover); color: var(--text-default); }
  .channel.on { background: var(--surface-selected); color: var(--text-strong); font-weight: 600; }
  .channel.unread { color: var(--text-strong); font-weight: 650; }
  .channel .hash { width: 14px; color: var(--text-faint); font-weight: 500; }
  .channel-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .me { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3); margin-top: var(--space-2); border-radius: var(--radius-lg); background: var(--surface-raised); border: 1px solid var(--border-subtle); box-shadow: var(--shadow-sm); }
  .me-text { flex: 1; min-width: 0; display: grid; line-height: 1.25; }
  .me-text strong { color: var(--text-strong); font-size: var(--text-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .status { display: inline-flex; align-items: center; gap: 6px; font-size: var(--text-xs); color: var(--text-muted); }
  .status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--online); }
  .status.offline::before { background: var(--amber-400); animation: pulse 1.2s ease-in-out infinite; }
  @keyframes pulse { 50% { opacity: 0.3; } }

  /* ---- room ---- */
  .room { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface-app); }
  .room-header { display: flex; align-items: center; gap: var(--space-3); height: 60px; padding: 0 var(--space-5); border-bottom: 1px solid var(--border-subtle); }
  .room-header .menu { display: none; }
  .room-title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: var(--space-3); }
  .room-title h1 { font-size: var(--text-lg); font-weight: 700; color: var(--text-strong); letter-spacing: -0.01em; white-space: nowrap; }
  .topic { color: var(--text-muted); font-size: var(--text-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .search { width: 220px; height: 34px; background: var(--surface-sunken); font-size: var(--text-sm); }

  .banner { padding: var(--space-2) var(--space-5); background: var(--mention-bg); color: var(--mention-text); font-size: var(--text-sm); font-weight: 500; border-bottom: 1px solid var(--border-subtle); }

  /* bottom-up, so the newest message stays in view without any scroll code */
  .messages { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column-reverse; padding: var(--space-4) 0 var(--space-2); }
  .messages .spacer { flex: 1; order: 99; }
  .empty { padding: var(--space-10) var(--space-6) var(--space-4); }
  .empty-title { font-size: var(--text-xl); font-weight: 700; color: var(--text-strong); letter-spacing: -0.02em; margin-bottom: var(--space-1); }

  .msg { position: relative; display: grid; grid-template-columns: 56px minmax(0, 1fr); padding: 2px var(--space-5) 2px 0; animation: enter var(--dur-med) var(--ease-out); }
  .msg.head { margin-top: var(--space-3); padding-top: var(--space-1); }
  .msg:hover { background: var(--surface-sunken); }
  .msg.mentions-me { background: var(--mention-bg); box-shadow: inset 3px 0 0 var(--mention-bar); }
  @keyframes enter { from { opacity: 0; transform: translateY(4px); } }
  .gutter { display: flex; justify-content: center; padding-top: 3px; }
  .hover-time { visibility: hidden; padding-top: 3px; font-size: 11px; color: var(--text-faint); font-variant-numeric: tabular-nums; }
  .msg:hover .hover-time { visibility: visible; }
  .msg header { display: flex; align-items: baseline; gap: var(--space-2); line-height: 1.3; }
  .author { font-weight: 650; color: var(--text-strong); }
  .msg header time { font-size: var(--text-xs); color: var(--text-faint); font-variant-numeric: tabular-nums; }
  .text { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text-default); }
  .text strong { color: var(--text-strong); font-weight: 650; }
  .mention { padding: 0 3px; border-radius: 4px; background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }
  .mention.me { background: var(--mention-bar); color: var(--stone-900); }

  .tools {
    position: absolute;
    top: -14px;
    right: var(--space-5);
    display: flex;
    padding: 2px;
    border-radius: var(--radius-md);
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-md);
    opacity: 0;
    pointer-events: none;
    transform: translateY(2px);
    transition: opacity var(--dur-fast), transform var(--dur-fast) var(--ease-out);
  }
  .msg:hover .tools, .tools:focus-within { opacity: 1; pointer-events: auto; transform: none; }
  .tool { width: 30px; height: 28px; border: 0; border-radius: var(--radius-sm); background: transparent; font-size: 15px; line-height: 1; }
  .tool:hover { background: var(--surface-hover); }
  .tool.danger { color: var(--text-muted); font-size: 12px; }
  .tool.danger:hover { color: var(--danger); }

  .reactions { display: flex; flex-wrap: wrap; gap: var(--space-1); margin-top: var(--space-1); }
  .chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 26px;
    padding: 0 var(--space-2);
    border-radius: var(--radius-full);
    border: 1px solid var(--border-subtle);
    background: var(--surface-raised);
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-muted);
    transition: border-color var(--dur-fast), background var(--dur-fast);
  }
  .chip:hover { border-color: var(--border-default); }
  .chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-text); }
  .chip .emoji { font-size: 14px; }
  .chip .count { font-variant-numeric: tabular-nums; }

  .preview {
    display: flex;
    gap: var(--space-4);
    max-width: 520px;
    margin-top: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--border-subtle);
    border-left: 3px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-raised);
    color: inherit;
    text-decoration: none;
    transition: border-color var(--dur-fast);
  }
  .preview:hover { border-left-color: var(--accent); }
  .preview-text { flex: 1; min-width: 0; display: grid; gap: 2px; align-content: start; }
  .preview-site { font-size: var(--text-xs); color: var(--text-muted); font-weight: 500; }
  .preview-title { color: var(--accent-text); font-size: var(--text-sm); line-height: 1.35; }
  .preview-desc { color: var(--text-muted); font-size: var(--text-sm); line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
  .preview-img { flex: none; width: 88px; height: 88px; object-fit: cover; border-radius: var(--radius-sm); background: var(--surface-sunken); }

  .composer { padding: 0 var(--space-5) var(--space-3); }
  .composer-box {
    display: flex;
    gap: var(--space-2);
    padding: var(--space-2);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-default);
    background: var(--surface-raised);
    box-shadow: var(--shadow-sm);
    transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
  }
  .composer-box:focus-within { border-color: var(--border-focus); box-shadow: 0 0 0 3px var(--accent-soft); }
  .composer-input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 0 var(--space-2); outline: none; color: var(--text-strong); }
  .composer-input::placeholder { color: var(--text-faint); }
  .composer-meta { display: flex; justify-content: space-between; gap: var(--space-4); min-height: 22px; padding: var(--space-1) var(--space-2) 0; font-size: var(--text-xs); color: var(--text-muted); }
  .typing { font-weight: 500; }
  .hint { color: var(--text-faint); white-space: pre; }

  /* ---- members ---- */
  .members { padding: var(--space-2) var(--space-2); background: var(--surface-app); border-left: 1px solid var(--border-subtle); overflow-y: auto; }
  .member { display: flex; align-items: center; gap: var(--space-3); padding: 5px var(--space-3); border-radius: var(--radius-md); font-size: var(--text-sm); font-weight: 500; }
  .member:hover { background: var(--surface-hover); }
  .presence { position: relative; display: inline-flex; }
  .presence::after { content: ""; position: absolute; right: -3px; bottom: -3px; width: 10px; height: 10px; border-radius: 50%; background: var(--online); border: 2px solid var(--surface-app); }
  .member-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-default); }
  .you { font-size: var(--text-xs); color: var(--text-faint); }

  .scrim { display: none; }

  /* ---- smaller screens ---- */
  @media (max-width: 1100px) {
    .shell { grid-template-columns: 248px minmax(0, 1fr); }
    .members { display: none; }
  }
  @media (max-width: 760px) {
    .shell { grid-template-columns: minmax(0, 1fr); }
    .sidebar {
      position: fixed;
      inset: 0 auto 0 0;
      z-index: 20;
      width: min(300px, 86vw);
      transform: translateX(-100%);
      transition: transform var(--dur-med) var(--ease-out);
      box-shadow: var(--shadow-lg);
    }
    .drawer-open .sidebar { transform: none; }
    .scrim { position: fixed; inset: 0; z-index: 10; background: rgb(12 10 9 / 0.4); opacity: 0; pointer-events: none; transition: opacity var(--dur-med); display: block; }
    .drawer-open .scrim { opacity: 1; pointer-events: auto; }
    .room-header { padding: 0 var(--space-3); }
    .room-header .menu { display: inline-grid; }
    .topic, .hint, .preview-img { display: none; }
    .search { width: 140px; }
    .msg { grid-template-columns: 48px minmax(0, 1fr); padding-right: var(--space-3); }
    .composer { padding: 0 var(--space-3) max(var(--space-3), env(safe-area-inset-bottom)); }
    .join-card { padding: var(--space-6); }
  }
}
