:root {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --ink: #1b2325;
  --muted: #62706f;
  --line: #e4ded2;
  --brand: #0f3d3e;
  --brand-ink: #ffffff;
  --accent: #e07a3f;
  --in: #ffffff;
  --out: #d9efe6;
  --stub: #eef0ef;
  --ok: #2f8f5b;
  --warn: #c98a12;
  --bad: #c2412d;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 4px 16px rgba(0, 0, 0, 0.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1416;
    --panel: #121e21;
    --ink: #e7eceb;
    --muted: #93a19f;
    --line: #22302f;
    --brand: #133e3f;
    --in: #172427;
    --out: #1d4a42;
    --stub: #1a2629;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
#app { height: 100vh; height: 100dvh; width: 100%; background: var(--bg); }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.screen { height: 100%; display: flex; flex-direction: column; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px 16px;
  background: var(--brand); color: var(--brand-ink);
  min-height: 60px; flex-shrink: 0;
}
.topbar .title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar .title strong { font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .title small { opacity: 0.75; font-size: 0.8rem; }
.topbar .actions { display: flex; gap: 4px; }
.icon-btn {
  background: none; border: 0; color: inherit; width: 40px; height: 40px;
  border-radius: 50%; font-size: 1.6rem; line-height: 1; cursor: pointer;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.15); }
.line-select {
  background: transparent; border: 0; color: var(--brand-ink); font-weight: 600;
  font-size: 1.05rem; max-width: 100%;
}
.line-select option { color: var(--ink); background: var(--panel); }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: 44px; z-index: 10; min-width: 220px;
  background: var(--panel); color: var(--ink); border-radius: 10px; box-shadow: var(--shadow);
  border: 1px solid var(--line); overflow: hidden;
}
.menu button { display: block; width: 100%; text-align: left; padding: 12px 16px; background: none; border: 0; cursor: pointer; }
.menu button:hover, .menu button:focus-visible { background: var(--stub); }

.banner {
  background: #fff4dc; color: #6b4a06; padding: 8px 16px; font-size: 0.9rem; border-bottom: 1px solid #f0dca8;
}
@media (prefers-color-scheme: dark) { .banner { background: #3a2e12; color: #f3d894; border-color: #4d3d18; } }

/* ---------- chat list ---------- */
.search-wrap { padding: 10px 12px; background: var(--bg); }
.search {
  width: 100%; padding: 10px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--panel);
}
.chat-list { list-style: none; margin: 0; padding: 0 0 96px; overflow-y: auto; flex: 1; background: var(--panel); }
.chat-row {
  display: flex; gap: 12px; padding: 10px 16px; text-decoration: none; color: inherit; align-items: center;
}
.chat-row:active { background: var(--stub); }
.chat-main { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: 10px; }
.row1, .row2 { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.row1 .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row1 time { font-size: 0.75rem; color: var(--muted); flex-shrink: 0; }
.row2 .preview { color: var(--muted); font-size: 0.92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row.unread time { color: var(--accent); font-weight: 600; }
.badge {
  background: var(--accent); color: #fff; border-radius: 999px; font-size: 0.75rem; font-weight: 700;
  min-width: 22px; height: 22px; padding: 0 6px; display: inline-flex; align-items: center; justify-content: center;
}
.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 700; color: #fff;
  background: hsl(var(--h, 180) 38% 42%); font-size: 1.05rem;
}
.empty { color: var(--muted); text-align: center; padding: 40px 16px; list-style: none; }
.fab {
  position: absolute; right: 20px; bottom: calc(env(safe-area-inset-bottom) + 20px);
  width: 58px; height: 58px; border-radius: 18px; border: 0; background: var(--accent); color: #fff;
  font-size: 2rem; line-height: 1; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2); cursor: pointer;
}

/* ---------- conversation ---------- */
.messages {
  flex: 1; overflow-y: auto; padding: 12px 10px 16px;
  display: flex; flex-direction: column; gap: 3px;
}
.day { text-align: center; margin: 12px 0 6px; }
.day span { background: var(--panel); color: var(--muted); font-size: 0.75rem; padding: 4px 10px; border-radius: 8px; box-shadow: var(--shadow); }
.msg {
  max-width: min(82%, 640px); padding: 7px 10px 5px; border-radius: var(--radius); box-shadow: var(--shadow);
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg.in { align-self: flex-start; background: var(--in); border-top-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-top-right-radius: 4px; }
.msg.stub { background: var(--stub); }
.msg .kind { color: var(--muted); font-style: italic; font-size: 0.92rem; }
.msg .sender { font-size: 0.8rem; font-weight: 700; color: hsl(var(--h, 180) 55% 38%); margin-bottom: 2px; }
@media (prefers-color-scheme: dark) { .msg .sender { color: hsl(var(--h, 180) 60% 70%); } }
.msg .meta { font-size: 0.7rem; color: var(--muted); text-align: right; margin-top: 2px; }
.msg .meta .by { margin-right: 6px; font-weight: 600; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 8px 10px calc(env(safe-area-inset-bottom) + 8px);
  background: var(--bg); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 22px; padding: 10px 16px;
  background: var(--panel); max-height: 140px; line-height: 1.35;
}
.send {
  width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--brand); color: #fff;
  font-size: 1.2rem; cursor: pointer; flex-shrink: 0;
}
.send:disabled { opacity: 0.5; }

/* ---------- forms / cards ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card, .card {
  background: var(--panel); border-radius: 18px; box-shadow: var(--shadow); padding: 22px;
  display: flex; flex-direction: column; gap: 14px; width: 100%;
}
.login-card { max-width: 380px; }
.card { margin: 14px 12px 0; width: auto; }
.login-card h1 { margin: 0 0 4px; font-size: 1.5rem; }
.brand-mark {
  width: 56px; height: 56px; border-radius: 16px; background: var(--brand); color: #fff;
  display: grid; place-items: center; font-size: 1.6rem;
}
label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; color: var(--muted); }
label input, .card input {
  padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink);
}
.btn {
  padding: 11px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel);
  font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--brand); color: #fff; border-color: transparent; }
.btn.danger { background: var(--bad); color: #fff; border-color: transparent; }
.btn:disabled { opacity: 0.6; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.error { color: var(--bad); margin: 0; min-height: 1.2em; font-size: 0.9rem; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }
.hint.pad { padding: 14px 16px 40px; }

.admin { overflow-y: auto; flex: 1; padding-bottom: 20px; }
.line-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.pill { font-size: 0.75rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; color: #fff; }
.pill.ok { background: var(--ok); }
.pill.warn { background: var(--warn); }
.pill.bad { background: var(--bad); }
.pairing { display: flex; flex-direction: column; gap: 10px; }
.pairing p { margin: 0; }
.qr { width: 260px; max-width: 100%; align-self: center; border-radius: 8px; background: #fff; padding: 8px; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form input { flex: 1; min-width: 140px; }
.inline-form .full { flex-basis: 100%; }
.pair-code { font-family: ui-monospace, monospace; font-size: 1.3rem; letter-spacing: 0.1em; margin: 0; }
.confirm { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 0.9rem; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translate(-50%, 20px); opacity: 0;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 0.9rem;
  transition: 0.2s; pointer-events: none; max-width: 90vw; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- pairing ---------- */
.pair { overflow-y: auto; flex: 1; padding-bottom: 24px; }
.pair .card { align-items: stretch; }
.pair .qr { width: 280px; }
.steps { margin: 0; padding-left: 1.3em; display: flex; flex-direction: column; gap: 6px; }
.center { text-align: center; }
.alt-link summary { cursor: pointer; color: var(--brand); font-weight: 600; padding: 4px 0; }
@media (prefers-color-scheme: dark) { .alt-link summary { color: #8fd3c4; } }
.alt-link[open] { display: flex; flex-direction: column; gap: 10px; }
.spinner {
  width: 36px; height: 36px; border-radius: 50%; align-self: center; margin: 12px 0 0;
  border: 4px solid var(--line); border-top-color: var(--brand); animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.menu-note { margin: 0; padding: 12px 16px 4px; color: var(--muted); font-size: 0.85rem; }
.menu-confirm { padding: 4px 16px 14px; display: flex; flex-direction: column; gap: 8px; }
.menu-confirm p { margin: 0; font-size: 0.9rem; }

/* ---------- install screen ---------- */
.install-card { text-align: left; }
.app-icon { width: 72px; height: 72px; border-radius: 18px; box-shadow: var(--shadow); }
.lead { margin: 0; font-size: 1.02rem; }
.btn.big { padding: 14px 18px; font-size: 1.05rem; }
.link-btn {
  background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer;
  padding: 6px 0 0; align-self: center; font-size: 0.9rem;
}
.pair-code.big {
  font-size: clamp(1.8rem, 9vw, 2.6rem); font-weight: 700; text-align: center; letter-spacing: 0.12em;
  background: var(--stub); border-radius: 12px; padding: 14px 8px; user-select: all;
}

/* ---------- responsive ---------- */
/* Side-by-side on wide screens (tablets, landscape phones, desktop). */
.split { display: grid; grid-template-columns: minmax(300px, 34%) 1fr; height: 100%; }
.split > .list-pane { border-right: 1px solid var(--line); min-width: 0; }
.split > .screen { min-width: 0; }
.split .topbar { padding-top: 10px; }
.chat-row.active { background: var(--stub); }
.empty-pane { display: grid; place-items: center; background: var(--bg); }
.empty-inner { text-align: center; color: var(--muted); padding: 24px; max-width: 360px; }
.empty-inner img { border-radius: 16px; opacity: 0.9; }
.empty-inner p { margin: 10px 0 0; }

/* Keep forms and cards a comfortable width on big screens. */
.pair .card, .screen > form.card { width: auto; max-width: 560px; margin-left: auto; margin-right: auto; }
@media (min-width: 600px) {
  .pair .card, .screen > form.card { margin-top: 24px; }
  .messages { padding-left: 4%; padding-right: 4%; }
  .composer { padding-left: 4%; padding-right: 4%; }
}
/* Short screens (phone in landscape): tighter header, smaller QR. */
@media (max-height: 500px) {
  .topbar { min-height: 48px; padding-top: calc(env(safe-area-inset-top) + 4px); padding-bottom: 4px; }
  .pair .qr { width: min(220px, 60vh); }
  .login { padding-block: 12px; }
}
/* Very narrow phones. */
@media (max-width: 360px) {
  .avatar { width: 42px; height: 42px; font-size: 0.95rem; }
  .chat-row { padding-left: 12px; padding-right: 12px; gap: 10px; }
  .login-card, .card { padding: 18px; }
}
/* Landscape/notch safe areas */
.topbar, .composer, .search-wrap { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }

/* ---------- photos ---------- */
.msg.has-photo { padding: 4px 4px 5px; }
.msg.has-photo .text, .msg.has-photo .meta, .msg.has-photo .sender { padding: 0 6px; }
.photo-btn { display: block; padding: 0; border: 0; background: none; cursor: zoom-in; width: 100%; }
.photo {
  display: block; width: min(300px, 100%); max-height: 420px; object-fit: cover; border-radius: 10px;
  background: var(--stub) center / cover no-repeat; min-height: 120px;
}
.attach {
  width: 46px; height: 46px; border-radius: 50%; border: 0; background: none; font-size: 1.35rem;
  cursor: pointer; flex-shrink: 0; color: var(--muted);
}
.attach:active { background: var(--stub); }
.viewer {
  position: fixed; inset: 0; z-index: 60; background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 52px) 12px calc(env(safe-area-inset-bottom) + 12px);
}
.viewer img { max-width: 100%; max-height: 100%; min-height: 0; flex: 1 1 auto; object-fit: contain; }
.viewer-close { position: absolute; top: calc(env(safe-area-inset-top) + 8px); right: 8px; color: #fff; font-size: 1.4rem; }
.viewer-caption { color: #fff; margin: 0; text-align: center; max-width: 640px; white-space: pre-wrap; }
.sheet .sheet-bar { width: 100%; max-width: 760px; background: transparent; border: 0; padding: 0; }
.sheet .caption-in {
  flex: 1; border-radius: 22px; border: 0; padding: 12px 16px; background: #fff; color: #111; min-width: 0;
}
.sheet .error { color: #ffb4a8; }

/* ---------- read receipts ---------- */
.ticks { margin-left: 4px; letter-spacing: -0.2em; font-size: 0.78rem; }
.ticks.read { color: #2a8bd6; font-weight: 700; }
.ticks.err { color: var(--bad); font-weight: 700; letter-spacing: 0; }
@media (prefers-color-scheme: dark) { .ticks.read { color: #53bdeb; } }
