@import url('https://fonts.googleapis.com/css2?family=Newsreader:ital,opsz,wght@0,6..72,400;0,6..72,500;0,6..72,600;1,6..72,400&family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500;600&family=Source+Serif+4:opsz,wght@8..60,400;8..60,500;8..60,600&family=Space+Grotesk:wght@400;500;600;700&display=swap');

:root {
  --ink: #14161A;
  --paper: #FAFAF8;
  --paper-raised: #FFFFFF;
  --line: #DDD9D0;
  --line-strong: #C7C2B6;
  --muted: #6B6B63;
  --accent: #1F4B3F;
  --accent-hover: #16362D;
  --accent-tint: #EAF0EE;
  --signal: #8A3324;
  --signal-tint: #F7EDEA;

  --serif: 'Newsreader', ui-serif, Georgia, serif;
  --sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  --radius-sm: 4px;
  --radius-md: 6px;
}

/* ---- Alternate themes (Settings > Theme) — each stays within the same
   design world (flat color, no gradients, one accent per screen); only
   the palette changes, never the structural principles. ---- */
html[data-theme="dusk"] {
  --ink: #EDEBE4;
  --paper: #17181B;
  --paper-raised: #1F2124;
  --line: #33353A;
  --line-strong: #45484E;
  --muted: #9B9A93;
  --accent: #4F9A82;
  --accent-hover: #6BB49C;
  --accent-tint: #1E2D28;
  --signal: #C2685A;
  --signal-tint: #2E2020;
}

html[data-theme="ledger"] {
  --ink: #2A2114;
  --paper: #F4EEDD;
  --paper-raised: #FBF7EC;
  --line: #DFD3B4;
  --line-strong: #CBBB8F;
  --muted: #7A6A4C;
  --accent: #7A4A24;
  --accent-hover: #603A1B;
  --accent-tint: #ECE0C6;
  --signal: #8A3324;
  --signal-tint: #F0DFCF;
}

/* ---- Alternate fonts (Settings > Font) — each option sets BOTH --serif
   and --sans, changing the WHOLE UI's typography (buttons, labels,
   messages, inputs — not just numeric/identity elements as an earlier
   version of this system did). Every pairing still respects the two-role
   split from DESIGN.md (one serif for numeric/identity elements, one
   sans for everything else) — only WHICH fonts fill those two roles
   changes, never the underlying structural principle. ---- */
html[data-font="mono"] {
  --serif: ui-monospace, 'IBM Plex Mono', 'SF Mono', Menlo, monospace;
  --sans: 'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, monospace;
}

html[data-font="classic-serif"] {
  --serif: 'Source Serif 4', ui-serif, Georgia, serif;
  --sans: 'Source Serif 4', ui-serif, Georgia, serif;
}

html[data-font="grotesk"] {
  --serif: 'Space Grotesk', -apple-system, sans-serif;
  --sans: 'Space Grotesk', -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  max-width: 620px;
  margin: 0 auto;
  padding: 1.25rem 1rem;
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0 0 0.4rem 0; font-weight: 600; letter-spacing: -0.01em; }

h1 { font-family: var(--serif); font-size: 1.6rem; font-weight: 500; }
h2 { font-size: 1.1rem; }
h3 { font-size: 0.95rem; color: var(--muted); text-transform: none; }

p { margin: 0; }

/* Numeric / identity elements — room codes, account tags, timestamps —
   always the serif, always tabular figures where supported. See
   DESIGN.md Principle 1. */
.numeric, .account-tag, .code-value {
  font-family: var(--serif);
  font-feature-settings: 'tnum' 1, 'lnum' 1;
  font-variant-numeric: tabular-nums lining-nums;
}

/* ---- Browser surfaces, themed rather than left default ---- */
::selection { background: var(--accent); color: var(--paper); }
::placeholder { color: #A6A196; }
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input:focus-visible { outline-offset: 0; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ---- Layout shells ---- */
#auth-view, #room-view, #manage-room-view {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  max-width: 360px;
  margin: 3rem auto;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 1.75rem 1.5rem;
}

/* ---- Form elements ---- */
input {
  font-family: var(--sans);
  font-size: 0.95rem;
  padding: 0.6rem 0.7rem;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--ink);
  width: 100%;
  transition: border-color 150ms ease;
}
input:hover { border-color: var(--line-strong); }
input:focus { border-color: var(--accent); }

label.field-label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
  font-weight: 500;
}

button {
  font-family: var(--sans);
  font-weight: 600;
  font-size: 0.85rem;
  padding: 0.6rem 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper-raised);
  color: var(--ink);
  cursor: pointer;
  transition: background-color 150ms ease, border-color 150ms ease, color 150ms ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}
button:hover { background: #F1EFE9; border-color: var(--line-strong); }
button:active { background: #E9E6DE; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button:disabled:hover { background: var(--paper-raised); }

/* Primary action — the ONE accent per screen. See DESIGN.md Principle 2. */
button.primary,
#auth-submit-btn, #join-btn, #create-btn, #send-btn,
#manage-change-password-btn, #manage-mint-token-btn,
#owner-key-close-btn {
  background: var(--accent);
  color: var(--paper);
  border-color: var(--accent);
}
button.primary:hover,
#auth-submit-btn:hover, #join-btn:hover, #create-btn:hover, #send-btn:hover,
#manage-change-password-btn:hover, #manage-mint-token-btn:hover,
#owner-key-close-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* Destructive actions — outlined, never filled solid, quieter than the
   accent by design. See DESIGN.md Principle 3 / Components. */
button.danger, #delete-account-btn {
  border-color: var(--signal);
  color: var(--signal);
  background: var(--paper-raised);
}
button.danger:hover, #delete-account-btn:hover { background: var(--signal-tint); }

.hint {
  font-size: 0.78rem;
  line-height: 1.55;
  color: var(--muted);
  margin: 0;
}

/* ---- Password visibility toggle ---- */
.password-field {
  position: relative;
  display: block;
}
.password-field input { padding-right: 2.4rem; width: 100%; }
.password-toggle {
  position: absolute;
  right: 0.4rem;
  top: 50%;
  transform: translateY(-50%);
  width: 1.9rem;
  height: 1.9rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
}
.password-toggle:hover { background: none; color: var(--ink); }
.password-toggle .icon { width: 1.05rem; height: 1.05rem; }


#auth-mode-toggle, #mode-toggle, #room-type-toggle {
  display: flex;
  gap: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
#auth-mode-toggle button, #mode-toggle button, #room-type-toggle button {
  flex: 1;
  background: var(--paper-raised);
  border: none;
  border-radius: 0;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 0.55rem 0.4rem;
}
#auth-mode-toggle button:not(:last-child), #mode-toggle button:not(:last-child), #room-type-toggle button:not(:last-child) {
  border-right: 1px solid var(--line);
}
#auth-mode-toggle button:hover, #mode-toggle button:hover, #room-type-toggle button:hover { background: #F1EFE9; }
#auth-mode-toggle button.mode-active, #mode-toggle button.mode-active, #room-type-toggle button.mode-active {
  background: var(--accent);
  color: var(--paper);
}

/* ---- Account bar ---- */
#account-bar {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--line);
}
#account-display { color: var(--ink); font-family: var(--serif); font-weight: 500; }
#account-bar button { font-size: 0.7rem; padding: 0.3rem 0.55rem; }

#join-panel, #create-panel {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
/* ---- Chat view ---- */
#chat-view {
  display: flex;
  flex-direction: column;
  height: 90vh;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  overflow: hidden;
}

header {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
}

#header-identity-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

#header-actions-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#room-name-display {
  font-family: var(--serif);
  font-size: 1rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 1;
  min-width: 0;
}

#room-code-display {
  font-family: var(--sans);
  color: var(--accent);
  font-weight: 600;
  font-size: 0.7rem;
  background: none;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
#room-code-display:hover { background: var(--accent-tint); }

#room-type-badge {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.15rem 0.45rem;
  white-space: nowrap;
  flex-shrink: 0;
}

#user-count {
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}

#leave-btn, #manage-room-btn, #manage-close-btn {
  font-size: 0.72rem;
  padding: 0.35rem 0.6rem;
  flex-shrink: 0;
}

/* Leave always sits at the far right of its own row; Manage (owner-only,
   when shown) sits just before it — this is the "wrap Leave below" fix:
   rather than let the whole header overflow/wrap unpredictably when an
   owner's extra Manage button doesn't fit, actions get their own
   dedicated row underneath the room identity row, every time. */
#leave-btn { margin-left: auto; }

#message-area {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
}

/* ---- Timestamp grouping — a divider between message clusters, not a
   repeated stamp on every bubble. ---- */
.time-divider {
  text-align: center;
  font-family: var(--serif);
  font-size: 0.72rem;
  color: var(--muted);
  margin: 1.1rem 0 0.6rem 0;
}
.time-divider:first-child { margin-top: 0; }

.message { margin-bottom: 0.15rem; max-width: 82%; }
.message + .message { margin-top: 0.15rem; }
.message.group-start { margin-top: 0.65rem; }

.message.system {
  color: var(--muted);
  font-size: 0.75rem;
  text-align: center;
  max-width: 100%;
  margin: 0.4rem 0;
  font-style: italic;
}

.message .sender {
  font-family: var(--serif);
  font-size: 0.74rem;
  font-weight: 500;
  margin-bottom: 0.15rem;
}

.message.self { text-align: right; margin-left: auto; }

.message.other div.bubble,
.message.self div.bubble {
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 0.5rem 0.7rem;
  font-size: 0.92rem;
  line-height: 1.45;
  word-wrap: break-word;
  display: inline-block;
  text-align: left;
  background: var(--paper-raised);
  user-select: text;
  -webkit-user-select: text;
}

.message.self div.bubble { background: var(--accent-tint); border-color: #C9DCD5; }

.message.other div.bubble.pressing,
.message.self div.bubble.pressing { border-color: var(--accent); }

.time { font-family: var(--sans); font-weight: 400; color: var(--muted); font-size: 0.68rem; margin-left: 0.35rem; }

.message.other, .message.self { cursor: pointer; }

.reply-quote {
  font-size: 0.74rem;
  color: var(--muted);
  border-left: 2px solid var(--line-strong);
  padding: 0.15rem 0.5rem;
  margin: 0.2rem 0;
  max-width: 100%;
}

/* ---- Markdown rendering inside message bubbles ---- */
.bubble code {
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 0.85em;
  background: rgba(0,0,0,0.05);
  border-radius: 3px;
  padding: 0.1em 0.35em;
}
.bubble pre {
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 0.82em;
  background: rgba(0,0,0,0.05);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.7rem;
  overflow-x: auto;
  margin: 0.35rem 0;
}
.bubble pre code { background: none; padding: 0; }
.bubble strong { font-weight: 700; }
.bubble em { font-style: italic; }
.bubble a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.bubble .mention {
  background: var(--accent-tint);
  color: var(--accent);
  border-radius: 3px;
  padding: 0.05em 0.3em;
  font-weight: 600;
}

/* ---- Typing indicator ---- */
#typing-indicator {
  font-size: 0.74rem;
  color: var(--muted);
  padding: 0.3rem 1rem;
  font-style: italic;
  min-height: 1.3em;
}
#typing-indicator .dots::after {
  content: '';
  display: inline-block;
  width: 1em;
  text-align: left;
  animation: typing-dots 1.4s steps(4, end) infinite;
}
@keyframes typing-dots {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
  100% { content: ''; }
}
@media (prefers-reduced-motion: reduce) {
  #typing-indicator .dots::after { animation: none; content: '...'; }
}

/* ---- @mention autocomplete ---- */
#mention-suggestions {
  position: absolute;
  bottom: 100%;
  left: 0.6rem;
  right: 0.6rem;
  background: var(--paper-raised);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  max-height: 160px;
  overflow-y: auto;
  display: none;
  margin-bottom: 0.3rem;
  box-shadow: 0 2px 10px rgba(20,22,26,0.08);
}
#mention-suggestions.visible { display: block; }
.mention-option { padding: 0.5rem 0.7rem; font-family: var(--serif); font-size: 0.85rem; cursor: pointer; }
.mention-option:hover, .mention-option.active { background: var(--accent-tint); }

#reply-preview {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--line);
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  color: var(--muted);
}
#reply-preview-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#reply-cancel-btn { background: none; border: none; cursor: pointer; font-size: 0.9rem; padding: 0 0.3rem; color: var(--signal); }

#input-row {
  display: flex;
  gap: 0.5rem;
  padding: 0.65rem;
  border-top: 1px solid var(--line);
  position: relative;
}
#input-row input { flex: 1; }

#auth-error, #room-error, #manage-password-error, #manage-tokens-error {
  color: var(--signal);
  font-size: 0.8rem;
  min-height: 1.2em;
}
#manage-password-success {
  color: var(--accent);
  font-size: 0.8rem;
  min-height: 1.2em;
}
#room-type-hint { min-height: 2.2em; }

/* ---- Copy feedback toast (long-press to copy) ---- */
#copy-toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--ink);
  color: var(--paper);
  font-size: 0.8rem;
  padding: 0.5rem 0.9rem;
  border-radius: var(--radius-sm);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 50;
}
#copy-toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- Owner key modal ---- */
#owner-key-modal {
  position: fixed;
  inset: 0;
  background: rgba(20,22,26,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 10;
}
#owner-key-box { background: var(--paper-raised); border-radius: var(--radius-md); padding: 1.75rem; max-width: 420px; width: 100%; }
#owner-key-value {
  font-family: var(--serif);
  font-feature-settings: 'tnum' 1;
  font-size: 0.9rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.7rem;
  margin: 0.7rem 0;
  word-break: break-all;
  user-select: all;
}
#owner-key-copy-btn { margin-right: 0.4rem; }

/* ---- Manage room panel ---- */
#manage-body { display: flex; flex-direction: column; gap: 1.1rem; }
#manage-body section { border-top: 1px solid var(--line); padding-top: 1rem; display: flex; flex-direction: column; gap: 0.55rem; }
#manage-body section:first-child { border-top: none; padding-top: 0; }

#manage-room-code-value, #manage-new-token-value {
  font-family: var(--serif);
  font-feature-settings: 'tnum' 1;
  font-size: 0.85rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.55rem;
  word-break: break-all;
  user-select: all;
}

#manage-mint-row { display: flex; gap: 0.4rem; align-items: flex-start; }
#manage-mint-row .password-field { flex: 1; min-width: 0; }

.token-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.5rem 0;
  border-top: 1px solid var(--line);
  font-size: 0.8rem;
}
.token-row:first-of-type { border-top: none; }
.token-row.revoked { color: var(--muted); }
.token-row button {
  font-size: 0.68rem;
  padding: 0.25rem 0.55rem;
  border-color: var(--signal);
  color: var(--signal);
  flex-shrink: 0;
}
.token-row-info { min-width: 0; flex: 1; }
.token-uses { font-size: 0.7rem; color: var(--muted); margin-top: 0.3rem; display: flex; flex-direction: column; gap: 0.15rem; }
.token-use-line .numeric { color: var(--ink); }
.token-row.revoked .token-use-line .numeric { color: var(--muted); }

/* ---- Settings ---- */
#settings-view {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  max-width: 420px;
  margin: 2.5rem auto;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 1.75rem 1.5rem;
}
#settings-view section { display: flex; flex-direction: column; gap: 0.6rem; border-top: 1px solid var(--line); padding-top: 1.1rem; }
#settings-view section:first-of-type { border-top: none; padding-top: 0; }

.theme-option-grid, .font-option-grid { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.option-chip {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.75rem;
  font-size: 0.78rem;
  cursor: pointer;
  background: var(--paper-raised);
}
.option-chip:hover { border-color: var(--line-strong); }
.option-chip.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }

#dev-about { font-size: 0.85rem; line-height: 1.6; color: var(--muted); }
#dev-about a { color: var(--accent); }

/* ---- Loading states ---- */
.spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  border: 2px solid rgba(20,22,26,0.2);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
  flex-shrink: 0;
}
button.primary .spinner { border-color: rgba(255,255,255,0.35); border-top-color: var(--paper); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 1400ms; } }

#page-loading {
  position: fixed;
  inset: 0;
  background: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#page-loading .spinner { width: 1.5rem; height: 1.5rem; border-width: 2px; border-color: var(--line-strong); border-top-color: var(--accent); }

/* ---- 404 / not-found ---- */
#not-found-view { max-width: 420px; margin: 4rem auto; text-align: left; display: flex; flex-direction: column; gap: 0.75rem; }
#not-found-view .code-value { font-size: 2.75rem; font-weight: 500; color: var(--muted); }

/* ---- Legal (privacy / terms) ---- */
#legal-view { max-width: 620px; margin: 1.5rem auto; line-height: 1.65; }
#legal-view h2 { font-family: var(--serif); font-size: 1.3rem; margin-top: 1.5rem; }
#legal-view h2:first-of-type { margin-top: 0; }
#legal-view p, #legal-view li { color: var(--muted); font-size: 0.9rem; }
#legal-view ul { padding-left: 1.2rem; }
#legal-view a { color: var(--accent); }

#consent-gate {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 420px;
  margin: 3rem auto;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 1.75rem 1.5rem;
}
#consent-gate label { display: flex; align-items: flex-start; gap: 0.55rem; font-size: 0.85rem; line-height: 1.5; cursor: pointer; }
#consent-gate input[type="checkbox"] { width: 1.05rem; height: 1.05rem; margin-top: 0.15rem; accent-color: var(--accent); flex-shrink: 0; }

/* ---- Icons: drawn SVG, one stroke weight, never emoji/unicode standing
   in for an icon system (see craft-floor.md). ---- */
.icon {
  width: 1em;
  height: 1em;
  flex-shrink: 0;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@media (max-width: 400px) {
  #auth-view, #room-view, #manage-room-view, #settings-view { margin: 1.5rem auto; padding: 1.25rem; }
  .message { max-width: 90%; }
  body { font-size: 14.5px; }
}

.cookie-notice{position:fixed;z-index:100;left:50%;bottom:18px;transform:translateX(-50%);width:min(680px,calc(100% - 28px));display:flex;align-items:center;justify-content:space-between;gap:18px;padding:14px 16px;border:1px solid var(--line-strong);border-radius:10px;background:var(--paper-raised);color:var(--ink);box-shadow:0 16px 40px rgba(0,0,0,.18);font-size:.82rem}.cookie-notice p{margin:.25rem 0 0;color:var(--muted);line-height:1.45}.cookie-notice a{color:var(--accent);text-underline-offset:3px}.cookie-notice button{flex:0 0 auto}@media(max-width:540px){.cookie-notice{align-items:stretch;flex-direction:column;gap:10px}.cookie-notice button{width:100%}}
