/* ==========================================================================
   Minimal Chat — shadcn-style design system, zero dependencies.

   The look follows shadcn/ui: a token layer plus component classes, with no
   framework and no build step. shadcn itself is React + Tailwind source code,
   so what is reproduced here is its design language, not its components:
   hairline borders, small radii, muted secondary text, a focus ring in
   --ring, and a neutral (zinc) palette expressed in plain hex so no
   oklch/color-mix support is required.

   Token names match shadcn (--background, --primary, --ring, --radius, ...)
   so themes can be swapped by overriding this block only.
   ========================================================================== */

:root {
  color-scheme: light;

  /* shadcn neutral (zinc) base */
  --background: #ffffff;
  --foreground: #0a0a0a;
  --card: #ffffff;
  --card-foreground: #0a0a0a;
  --popover: #ffffff;
  --popover-foreground: #0a0a0a;
  --primary: #171717;
  --primary-foreground: #fafafa;
  --secondary: #f4f4f5;
  --secondary-foreground: #171717;
  --muted: #f4f4f5;
  --muted-foreground: #71717a;
  --accent: #f4f4f5;
  --accent-foreground: #171717;
  --destructive: #dc2626;
  --destructive-foreground: #fafafa;
  --success: #16a34a;
  --warning: #d97706;
  --border: #e4e4e7;
  --input: #e4e4e7;
  --ring: #a1a1aa;

  --radius: 0.625rem;
  --radius-sm: calc(var(--radius) - 4px);
  --radius-md: calc(var(--radius) - 2px);
  --radius-lg: var(--radius);
  --radius-xl: calc(var(--radius) + 4px);

  /* app-specific accents derived from tokens */
  --sidebar: #fafafa;
  --sidebar-hover: #f4f4f5;
  --bubble: #ffffff;
  --bubble-mine: #f4f4f5;
  --bubble-ink: #0a0a0a;
  --bubble-ink-mine: #0a0a0a;
  --quote-bg: #fafafa;
  --header: #171717;
  --header-ink: #fafafa;

  --font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif,
    "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  color-scheme: dark;

  --background: #09090b;
  --foreground: #fafafa;
  --card: #18181b;
  --card-foreground: #fafafa;
  --popover: #18181b;
  --popover-foreground: #fafafa;
  --primary: #fafafa;
  --primary-foreground: #171717;
  --secondary: #27272a;
  --secondary-foreground: #fafafa;
  --muted: #27272a;
  --muted-foreground: #a1a1aa;
  --accent: #27272a;
  --accent-foreground: #fafafa;
  --destructive: #ef4444;
  --destructive-foreground: #fafafa;
  --success: #22c55e;
  --warning: #f59e0b;
  --border: #27272a;
  --input: #27272a;
  --ring: #71717a;

  --sidebar: #0f0f11;
  --sidebar-hover: #1c1c1f;
  --bubble: #18181b;
  --bubble-mine: #27272a;
  --bubble-ink: #fafafa;
  --bubble-ink-mine: #fafafa;
  --quote-bg: #101013;
  --header: #18181b;
  --header-ink: #fafafa;
}

/* ---------- base ---------- */

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font-sans);
  font-size: 0.9375rem;
  line-height: 1.5;
  background: var(--background);
  color: var(--foreground);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

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

.hidden { display: none !important; }

/* ---------- components ---------- */

/* shadcn Button: variants map to the token pairs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  white-space: nowrap;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.25rem;
  cursor: pointer;
  transition: background-color .15s, color .15s, border-color .15s, opacity .15s;
  background: var(--primary);
  color: var(--primary-foreground);
}
.btn:hover { opacity: .9; }
.btn:disabled { pointer-events: none; opacity: .5; }

.btn-secondary { background: var(--secondary); color: var(--secondary-foreground); }
.btn-outline { background: transparent; color: var(--foreground); border-color: var(--border); }
.btn-outline:hover { background: var(--accent); color: var(--accent-foreground); }
.btn-ghost { background: transparent; color: var(--foreground); }
.btn-ghost:hover { background: var(--accent); color: var(--accent-foreground); }
.btn-destructive { background: var(--destructive); color: var(--destructive-foreground); }
.btn-sm { padding: 0.25rem 0.625rem; font-size: 0.8125rem; border-radius: var(--radius-sm); }
.btn-icon { width: 2rem; height: 2rem; padding: 0; border-radius: var(--radius-md); }
.btn-block { width: 100%; }
/* The submit button is a direct child of the card <form>, so it needs its own
   space above it: with no margin it touched the field above it. */
.card .btn-block { margin-top: 1.25rem; }

/* shadcn Input */
.input {
  display: flex;
  width: 100%;
  border-radius: var(--radius-md);
  border: 1px solid var(--input);
  background: transparent;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  /* Match .btn's line-height, otherwise inputs come out 34px tall next to
     38px buttons in the same form. */
  line-height: 1.25rem;
  font-family: inherit;
  color: var(--foreground);
  transition: border-color .15s, box-shadow .15s;
}
.input::placeholder { color: var(--muted-foreground); }
.input:focus-visible {
  outline: none;
  border-color: var(--ring);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 35%, transparent);
}

/* shadcn Card */
.card {
  background: var(--card);
  color: var(--card-foreground);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  width: 100%;
  max-width: 22rem;
  box-shadow: 0 1px 2px rgb(0 0 0 / .04);
}
.card h1 { font-size: 1.25rem; font-weight: 600; letter-spacing: -.01em; }
.card .sub { color: var(--muted-foreground); font-size: .875rem; margin-top: 0.375rem; margin-bottom: 1rem; }
.card.wide-card { max-width: 44rem; }

.label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--foreground);
  /* 16px between fields, 6px between a label and its own input. Without this
     rule the labels carried no margin at all and consecutive fields sat 2px
     apart, so a stack of inputs read as one undivided block. */
  margin: 1rem 0 0.375rem;
}

/* shadcn Alert */
.alert {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  padding: 0.625rem 0.875rem;
  font-size: 0.8125rem;
  margin-top: 1rem;
  background: var(--muted);
  color: var(--foreground);
}
.alert-destructive {
  background: var(--destructive);
  color: var(--destructive-foreground);
  border-color: transparent;
}

/* shadcn Badge / Separator / muted text */
.badge {
  display: inline-flex;
  align-items: center;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  padding: 0 0.5rem;
  height: 1.25rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--foreground);
  background: var(--secondary);
}
.separator { height: 1px; background: var(--border); border: 0; }
.muted { color: var(--muted-foreground); }

.center {
  min-height: 100vh;
  /* iOS reports 100vh taller than what is on screen while its toolbar is
     expanded, which would centre the card partly behind the browser UI. */
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.switch { margin-top: 1rem; font-size: .8125rem; text-align: center; color: var(--muted-foreground); }
.switch a { color: var(--foreground); text-decoration: underline; text-underline-offset: 3px; }

/* floating theme toggle on the auth pages */
.theme-float {
  position: fixed;
  top: 0.875rem;
  right: 0.875rem;
  z-index: 20;
}

/* ---------- app: chat ---------- */

.chat-wrap {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 60rem;
  margin: 0 auto;
  background: var(--background);
}
.chat-wrap.wide { max-width: 68rem; }

/* The header does NOT invert with the theme: --header is near-black in both of
   them, so every control inside it has to take --header-ink. Inheriting
   --foreground (as .btn-ghost does) put near-black ink on a near-black header
   in light mode and the theme toggle rendered no distinguishable pixel at all,
   measured contrast 1.0 -- present in the DOM, clickable, and invisible.

   Hover is a hairline rather than a background wash. The logout button in this
   same header already uses a 1px border in --header-ink as its affordance, and
   neither obvious alternative survives: --accent is a light surface in light
   mode and would swallow light ink, while a white wash over a near-black
   header drops the label to 3.5:1 while hovered. A border costs the label
   nothing. */
.chat-header .btn-ghost { color: var(--header-ink); }
.chat-header .btn-ghost:hover {
  background: transparent;
  border-color: var(--header-ink);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 1rem;
  background: var(--header);
  color: var(--header-ink);
  border-bottom: 1px solid var(--border);
}
.chat-header .me {
  font-weight: 500;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-header .logout {
  background: transparent;
  color: var(--header-ink);
  border: 1px solid var(--header-ink);
  opacity: .85;
  border-radius: var(--radius-md);
  padding: 0.375rem 0.75rem;
  font-size: .8125rem;
  font-weight: 500;
}
.chat-header .logout:hover { background: var(--header-ink); color: var(--header); opacity: 1; }
/* No .chat-header .icon-btn override: it repeated what .btn-ghost already
   sets and, sitting later at the same specificity, silently replaced the hover
   wash above. Duplicating a rule to restate it is how that happened. */

.layout { display: flex; flex: 1; min-height: 0; }

.sidebar {
  width: 15rem;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  padding: 0.75rem;
  overflow-y: auto;
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--background);
}

.conv {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  text-align: left;
  padding: 0.5rem 0.625rem;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--foreground);
  font-size: 0.875rem;
  cursor: pointer;
  overflow: hidden;
}
/* The label owns the ellipsis: overflow on the flex container would clip the
   badge instead of truncating a long username. */
.conv-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv:hover { background: var(--sidebar-hover); }
.conv.active { background: var(--secondary); color: var(--secondary-foreground); font-weight: 500; }

/* Presence dot. Decorative only -- the row's accessible name says nothing
   about being online, so this must not depend on colour alone to be useful:
   it is a shape cue next to the name, not the sole signal. */
.conv-online {
  flex: none;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: var(--primary);
}

/* Token pair, so the badge inverts with the theme on its own -- the same
   reason it is not a fixed colour. */
.conv-badge {
  flex: none;
  min-width: 1.125rem;
  height: 1.125rem;
  padding: 0 0.25rem;
  border-radius: 999px;
  background: var(--primary);
  color: var(--primary-foreground);
  font-size: 0.625rem;
  font-weight: 600;
  line-height: 1.125rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.side-title {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted-foreground);
  margin-top: 0.25rem;
}

.dm-start { display: flex; gap: 0.375rem; }
.dm-start input { flex: 1; min-width: 0; }
#dm-error { margin-top: 0.25rem; }

.status {
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted-foreground);
  padding: 0.25rem;
}

.view-title {
  padding: 0.5rem 1rem 0;
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--foreground);
}

#messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.msg {
  background: var(--bubble);
  color: var(--bubble-ink);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.5rem 0.75rem;
  /* Cap the line length as well as the share of the pane. The app frame is
     already capped at 68rem, so at 1440px an 85% bubble still reaches ~694px
     (about 95 characters per line); 36rem keeps it near 80. Short messages
     stay short because the bubble is fit-content. */
  max-width: min(85%, 36rem);
  width: fit-content;
  word-wrap: break-word;
}
.msg.mine {
  align-self: flex-end;
  background: var(--bubble-mine);
  color: var(--bubble-ink-mine);
}
.msg .meta { font-size: 0.6875rem; color: var(--muted-foreground); margin-bottom: 1px; }
.msg .meta strong { color: inherit; font-weight: 600; }

/* An optimistic row that has not been echoed yet. Faded, so the difference
   from a delivered message reads at a glance without needing a label. */
.msg.pending { opacity: 0.6; }

.quote {
  display: block;
  width: 100%;
  min-height: 1.75rem;
  text-align: left;
  border: 0;
  border-left: 2px solid var(--ring);
  background: var(--quote-bg);
  color: var(--muted-foreground);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: 0.25rem 0.5rem;
  margin-bottom: 0.375rem;
  font-size: 0.8125rem;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.quote:hover { color: var(--foreground); }
.quote .q-who { font-weight: 600; color: var(--foreground); }

.reply-btn {
  /* A real tap target: 28px tall, not a 14px sliver of text. */
  display: inline-flex;
  align-items: center;
  min-height: 1.75rem;
  padding: 0 0.375rem;
  margin: 0.25rem 0 -0.25rem -0.375rem;
  border-radius: calc(var(--radius) - 1px);
  background: none;
  border: none;
  color: var(--muted-foreground);
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s, color .15s;
}
.msg:hover .reply-btn, .reply-btn:focus-visible { opacity: 1; }
.reply-btn:hover { color: var(--foreground); background: var(--accent); }
.reply-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--ring);
  color: var(--foreground);
}

.msg.flash { animation: msgflash 2.2s ease; }
@keyframes msgflash {
  0%, 100% { border-color: var(--border); }
  20%, 65% { border-color: var(--ring); }
}

.composer {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem;
  border-top: 1px solid var(--border);
  background: var(--background);
}
.composer input { flex: 1; }

.reply-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-top: 1px solid var(--border);
  background: var(--muted);
  font-size: 0.8125rem;
}
.reply-bar.hidden { display: none; }
.reply-bar span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted-foreground); }
.reply-bar button { background: transparent; border: none; color: var(--foreground); cursor: pointer; font-size: 1rem; line-height: 1; }

/* ---------- sidebar drawer (small screens) ---------- */
/* On phones the conversation list becomes an overlay drawer, so the message
   area keeps the full width instead of losing half the screen to it. */
.sidebar-toggle { display: none; }

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgb(0 0 0 / 0.4);
}

.sidebar-open .sidebar-backdrop { display: block; }

@media (max-width: 600px) {
  .sidebar-toggle { display: inline-flex; }

  .sidebar {
    position: fixed;
    z-index: 40;
    top: 0;
    left: 0;
    /* Never taller than the visible viewport (iOS shrinks vh as its toolbar
       hides), otherwise the drawer becomes a second scroll trap. */
    height: 100vh;
    height: 100dvh;
    width: min(17rem, 84vw);
    /* No header offset: the backdrop already covers the header, so tapping it
       is the way out and the drawer needs no dead strip at the top. */
    border-right: 1px solid var(--border);
    box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }

  .sidebar-open .sidebar { transform: none; }
  .sidebar-open .chat-wrap { overflow: hidden; }
}

/* ---------- app: server identity panel ---------- */
.identity {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--muted);
  /* 16px, so the panel sits on the same rhythm as the form fields above it. */
  margin: 1rem 0 0;
  font-size: 0.8125rem;
}

.identity-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: none;
  border: 0;
  border-radius: inherit;
  color: var(--foreground);
  font: inherit;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
}

.identity-toggle:hover { background: var(--accent); }
.identity-toggle:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 35%, transparent);
}

.identity-toggle > span:first-child { white-space: nowrap; }

.identity-toggle-hint {
  color: var(--muted-foreground);
  font-weight: 400;
  font-size: 0.75rem;
  text-align: right;
}
.identity-toggle-hint.is-bad { color: var(--destructive); font-weight: 600; }

.identity-body { padding: 0 0.75rem 0.75rem; }

/* Key and action share the first row; the fingerprint takes the full width on
   the second. As a flex row it was squeezed into a ~180px column and broke
   after every group of four, which made the value unreadable on a phone. */
.identity-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.25rem 0.5rem;
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
}
.identity-row:first-child { border-top: 0; }

/* Without a button beside it, a mismatched pin needs to read as a problem on
   its own row rather than as slightly red text. */
.identity-row.is-stale {
  background: color-mix(in srgb, var(--destructive) 8%, transparent);
  margin-inline: -0.375rem;
  padding-inline: 0.375rem;
  border-radius: calc(var(--radius) - 1px);
}

.identity-key { grid-column: 1; grid-row: 1; color: var(--muted-foreground); }
.identity-row .btn { grid-column: 2; grid-row: 1; justify-self: end; }

.identity-value {
  grid-column: 1 / -1;
  grid-row: 2;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.75rem;
  line-height: 1.5;
  /* A 64-hex fingerprint is 95 characters grouped; it must break rather than
     push the card wider on a phone. */
  word-break: break-all;
  user-select: all;
}
.identity-value.is-bad { color: var(--destructive); font-weight: 600; }

.identity-form {
  display: flex;
  /* The value being pasted is 95 characters; a full-width input on a desktop
     would be far wider than the text it is for. */
  max-width: 34rem;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.375rem;
  margin-top: 0.5rem;
}
.identity-form .label { flex: 1 0 100%; margin-top: 0.5rem; }
.identity-input { flex: 1 1 12rem; min-width: 0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.75rem; }
.identity-form .btn { flex-shrink: 0; }

.identity-result { margin-top: 0.5rem; }
.identity-result.is-ok { color: var(--foreground); font-weight: 500; }
.identity-result.is-bad { color: var(--destructive); font-weight: 600; }

.identity-hint { margin-top: 0.5rem; color: var(--muted-foreground); font-size: 0.75rem; }

/* ---------- app: status strips ---------- */

.security {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 1rem;
  background: var(--muted);
  border-bottom: 1px solid var(--border);
  font-size: 0.6875rem;
  color: var(--muted-foreground);
}
.security.hidden { display: none; }
.security .fp { font-family: var(--font-mono); letter-spacing: .02em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.security .sec-copy { background: none; border: none; color: inherit; font-size: inherit; cursor: pointer; text-decoration: underline; }
.security .sec-changed { color: var(--destructive); font-weight: 700; }
.security.warn { background: var(--destructive); color: var(--destructive-foreground); }

.split-warning {
  padding: 0.625rem 1rem;
  background: var(--warning);
  color: #1a1a1a;
  font-size: 0.8125rem;
  font-weight: 600;
}
.split-warning.hidden { display: none; }

/* ---------- app: admin ---------- */

.admin-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 0.75rem; }
.admin-head .sub { margin-bottom: 0; }
.admin-add { display: flex; flex-wrap: wrap; gap: 0.375rem; margin: 1rem 0 0.5rem; }
.admin-add input { flex: 1 1 8rem; min-width: 0; }
#panel-error { margin: 0.5rem 0; }

.user-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  align-items: center;
  padding: 0.625rem 0;
  border-top: 1px solid var(--border);
}
.user-row .user-info { flex: 1 1 100%; font-size: 0.875rem; }
.user-row .muted { color: var(--muted-foreground); font-size: 0.75rem; }
.user-row input { flex: 1 1 8rem; min-width: 0; }

/* ---------- responsive ---------- */

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}

@media (max-width: 600px) {
  .chat-wrap, .chat-wrap.wide { height: 100dvh; }
  .card, .card.wide-card { padding: 1.25rem; }
  .layout { flex-direction: column; }
  .msg { max-width: 92%; }
  .reply-btn { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  .msg.flash { border-color: var(--ring); }
}