:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --ink: #1c1b19;
  --muted: #6b6760;
  --line: #e4e1da;
  --accent: #8a6a3b;
  --accent-ink: #ffffff;
  --accent-soft: #f1e9dc;
  --out: #efe6d6;
  --in: #ffffff;
  --ok: #2f6b45;
  --bad: #a3382d;
  --warn: #8a5a12;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141412;
    --panel: #1d1c1a;
    --ink: #ecebe7;
    --muted: #a39f97;
    --line: #2f2d29;
    --accent: #c9a46b;
    --accent-ink: #1a1408;
    --accent-soft: #2b2419;
    --out: #2d2619;
    --in: #232220;
    --ok: #7cc49a;
    --bad: #f08a7e;
    --warn: #e0b765;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 16px; margin: 28px 0 10px; font-weight: 650; }
p { margin: 0 0 10px; }
small, .muted { color: var(--muted); }
code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--accent-soft); padding: 1px 5px; border-radius: 6px; }

/* Top bar */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 18px;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--ink); white-space: nowrap; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a { color: var(--muted); padding: 6px 10px; border-radius: 8px; white-space: nowrap; }
.nav a:hover { text-decoration: none; color: var(--ink); }
.nav a.on { color: var(--ink); background: var(--accent-soft); }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 4px;
  border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  font-size: 12px; font-weight: 700; text-align: center; line-height: 20px;
}
.badge[hidden] { display: none; }
.who { display: flex; align-items: center; gap: 10px; color: var(--muted); white-space: nowrap; }

/* Layout */
main { max-width: 980px; margin: 0 auto; padding: 24px 20px 80px; }
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.panel > h2:first-child { margin-top: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stack > * + * { margin-top: 12px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat b { display: block; font-size: 22px; font-weight: 650; }
.stat small { display: block; }

/* Flash */
.flash { padding: 10px 14px; border-radius: 10px; margin-bottom: 16px; border: 1px solid var(--line); background: var(--panel); }
.flash.ok { border-color: var(--ok); }
.flash.bad { border-color: var(--bad); color: var(--bad); }

/* Forms */
label { display: block; font-weight: 550; margin: 14px 0 6px; }
label small { font-weight: 400; }
input[type=text], input[type=url], input[type=search], input[type=password], select, textarea {
  width: 100%; padding: 10px 12px; font: inherit; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
}
textarea { resize: vertical; min-height: 90px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; font: inherit; font-weight: 600; cursor: pointer;
  color: var(--accent-ink); background: var(--accent); border: 1px solid var(--accent); border-radius: 10px;
}
.btn:hover { text-decoration: none; filter: brightness(1.05); }
.btn.ghost { color: var(--ink); background: transparent; border-color: var(--line); }
.btn.danger { color: var(--bad); background: transparent; border-color: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.checks { display: flex; flex-wrap: wrap; gap: 6px; }
.checks label { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; margin: 0;
  padding: 5px 10px; border: 1px solid var(--line); border-radius: 999px; background: var(--panel); cursor: pointer; }
.checks input { margin: 0; accent-color: var(--accent); }
.inline { display: inline; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.list a.item { display: flex; gap: 12px; align-items: center; padding: 12px 14px; color: var(--ink); }
.list a.item:hover { text-decoration: none; background: var(--accent-soft); }
.avatar { flex: none; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.item .main { flex: 1; min-width: 0; }
.item .line1 { display: flex; justify-content: space-between; gap: 8px; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .when { color: var(--muted); font-size: 13px; white-space: nowrap; }
.item .last { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.item.unread .name, .item.unread .last { color: var(--ink); font-weight: 650; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 9px 12px; border-top: 1px solid var(--line); vertical-align: top; }
th { border-top: 0; font-weight: 600; color: var(--muted); font-size: 13px; }
.wrap { overflow-x: auto; }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600;
  border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.pill.ok { color: var(--ok); border-color: var(--ok); }
.pill.bad { color: var(--bad); border-color: var(--bad); }
.pill.warn { color: var(--warn); border-color: var(--warn); }
.pill.accent { color: var(--accent); border-color: var(--accent); }

/* Conversation */
.thread-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.thread-head .main { flex: 1; min-width: 0; }
.msgs { display: flex; flex-direction: column; gap: 8px; padding: 6px 0 16px; }
.bubble { max-width: min(78%, 560px); padding: 8px 12px; border-radius: 14px; border: 1px solid var(--line);
  overflow-wrap: anywhere; }
.bubble .text { white-space: pre-wrap; }
.bubble.in { align-self: flex-start; background: var(--in); border-bottom-left-radius: 4px; }
.bubble.out { align-self: flex-end; background: var(--out); border-bottom-right-radius: 4px; }
.bubble .meta { display: block; margin-top: 4px; font-size: 12px; color: var(--muted); text-align: right; white-space: normal; }
.bubble.in .meta { text-align: left; }
.bubble .tag { font-size: 11px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: .04em; display: block; margin-bottom: 2px; }
.bubble img { display: block; max-width: 100%; border-radius: 8px; margin-bottom: 4px; }
.bubble .err { color: var(--bad); }
.day { align-self: center; font-size: 12px; color: var(--muted); padding: 2px 10px; }
.composer { position: sticky; bottom: 0; background: var(--bg); padding: 10px 0 4px; }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 46px; max-height: 200px; }
.notice { padding: 12px 14px; border: 1px dashed var(--line); border-radius: 10px; background: var(--panel); }
.newpill { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); }
.preview { white-space: pre-wrap; background: var(--out); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; max-width: 520px; }

/* Templates */
.list li.item { display: flex; gap: 12px; align-items: center; padding: 12px 14px; }
.item .preview-line { display: block; margin-top: 2px; }
.split { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 18px; align-items: start; }
.split .sticky { position: sticky; top: 16px; }
.split h2 { margin-top: 0; }
#tpreview { white-space: normal; }
#tpreview .ph { font-weight: 700; margin-bottom: 6px; }
#tpreview .pmedia { height: 120px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); display: grid;
  place-items: center; font-weight: 600; margin-bottom: 8px; }
#tpreview .pbody { white-space: pre-wrap; }
#tpreview .pfoot { color: var(--muted); font-size: 13px; margin-top: 6px; }
#tpreview .pbtn { text-align: center; color: var(--accent); font-weight: 600; border-top: 1px solid var(--line);
  margin: 8px -14px 0; padding: 8px 14px 0; }
.flash.stack > * + * { margin-top: 6px; }

.bar { height: 8px; background: var(--accent-soft); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }

.codebox { font: 22px ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .08em; padding: 12px 16px;
  border: 2px solid var(--accent); border-radius: 12px; display: inline-block; background: var(--panel); }

/* Small utilities (no inline styles: the CSP blocks them) */
.accent { color: var(--accent); }
.full { width: 100%; }
.hint { margin-top: 8px; }
.search { min-width: 240px; }
.mt { margin-top: 14px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* Login */
.login { max-width: 360px; margin: 12vh auto 0; }
.login .panel { padding: 24px; }

@media (max-width: 640px) {
  .top { padding: 8px 12px; gap: 6px 10px; flex-wrap: wrap; }
  .brand { flex: 1; }
  .nav { order: 3; flex-basis: 100%; }
  .who .name { display: none; }
  main { padding: 16px 12px 80px; }
  .bubble { max-width: 88%; }
  th, td { padding: 8px; }
  .split { grid-template-columns: 1fr; }
  .split .sticky { position: static; }
}
