:root {
  --bg: #07060a; --panel: #100d0b; --panel2: #17120e; --line: rgba(224,168,75,0.22); --gold: #e0a84b; --gold-hi: #f6d58e;
  --ink: #ede6da; --dim: rgba(237,230,218,0.62); --in: #1b1612; --out: #3a2a12; --wa: #25d366; --em: #6ea8ff; --err: #ff8a7a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font: 15px/1.55 'Martel', system-ui, sans-serif; }
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--dim); }
.error { color: var(--err); min-height: 1.2em; margin: 6px 0 0; font-size: 14px; }
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 16px; cursor: pointer; color: var(--gold-hi); white-space: nowrap; }
.btn:hover { border-color: var(--gold); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn--gold { background: linear-gradient(180deg, #f2c66f, #c8892c); color: #1a1006; border: 0; font-weight: 700; }
.btn--ghost { background: none; }
.btn--small { padding: 4px 10px; font-size: 13px; }
.icon-btn { background: none; border: 1px solid var(--line); border-radius: 9px; min-width: 36px; height: 36px; padding: 0 8px; cursor: pointer; color: var(--gold-hi); white-space: nowrap; }
code { background: var(--panel2); padding: 2px 6px; border-radius: 5px; font-size: 13px; word-break: break-all; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login__card { width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; display: grid; gap: 12px; text-align: center; }
.login__card h1 { font-family: 'Cinzel', serif; color: var(--gold-hi); margin: 0; letter-spacing: 0.08em; }
.login__card label, .stack label { display: grid; gap: 4px; text-align: left; font-size: 14px; color: var(--dim); }
input, textarea, select { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; outline: none; }
input:focus, textarea:focus, select:focus { border-color: var(--gold); }

/* layout */
.app { display: grid; grid-template-columns: 360px 1fr; height: 100dvh; }
.side { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.side__top { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-family: 'Cinzel', serif; color: var(--gold-hi); letter-spacing: 0.12em; }
.side__actions { display: flex; gap: 6px; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 12px 4px; }
.chip { border: 1px solid var(--line); background: none; border-radius: 999px; padding: 3px 12px; cursor: pointer; font-size: 13px; color: var(--dim); }
.chip.is-on { color: #1a1006; background: var(--gold); border-color: var(--gold); }
.chip span:not(:empty) { margin-left: 4px; font-weight: 700; }
.chip--toggle { display: inline-flex; gap: 6px; align-items: center; }
.search { margin: 8px 12px; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.item { display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; padding: 11px 14px; border-bottom: 1px solid rgba(224,168,75,0.08); cursor: pointer; }
.item:hover, .item.is-on { background: var(--panel2); }
.item__icon { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-size: 15px; font-weight: 700; color: #071007; }
.item__icon.wa { background: var(--wa); }
.item__icon.em { background: var(--em); color: #06101e; }
.item__main { min-width: 0; }
.item__name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item__prev { color: var(--dim); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item__meta { text-align: right; font-size: 12px; color: var(--dim); }
.unread { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--gold); color: #1a1006; font-weight: 700; text-align: center; margin-top: 4px; }
.empty { padding: 20px; text-align: center; }

.thread { min-width: 0; min-height: 0; display: flex; }
.placeholder { margin: auto; text-align: center; color: var(--dim); opacity: 0.8; }
.pane { display: flex; flex-direction: column; width: 100%; min-height: 0; }
.pane__top { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.back { display: none; }
.who { flex: 1; min-width: 0; }
.who__name { background: none; border: 1px solid transparent; padding: 2px 6px; font-weight: 700; font-size: 17px; width: 100%; }
.who__name:hover, .who__name:focus { border-color: var(--line); }
.who__addr { color: var(--dim); font-size: 13px; padding-left: 6px; }
.badge { font-size: 11px; padding: 1px 8px; border-radius: 999px; font-weight: 700; }
.badge.wa { background: var(--wa); color: #071007; }
.badge.em { background: var(--em); color: #06101e; }
.window { font-size: 12px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); white-space: nowrap; }
.window.open { color: var(--wa); border-color: rgba(37,211,102,0.5); }
.window.closed { color: var(--err); border-color: rgba(255,138,122,0.5); }
.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: min(680px, 86%); padding: 9px 13px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--in); border: 1px solid rgba(224,168,75,0.12); }
.msg.out { align-self: flex-end; background: var(--out); }
.msg__subject { font-weight: 700; color: var(--gold-hi); margin-bottom: 4px; }
.msg__meta { font-size: 11px; color: var(--dim); margin-top: 4px; text-align: right; }
.msg__meta .ai { color: var(--gold); font-weight: 700; }
.msg__meta .failed { color: var(--err); }
.msg img { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-top: 6px; }
.msg a { color: var(--gold-hi); }
.day { align-self: center; font-size: 12px; color: var(--dim); padding: 2px 10px; border: 1px solid var(--line); border-radius: 999px; }
.composer { border-top: 1px solid var(--line); padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); display: grid; gap: 8px; }
.row { display: flex; gap: 8px; align-items: flex-end; }
.row textarea { flex: 1; resize: vertical; min-height: 44px; max-height: 40vh; }
.subject { width: 100%; }
.template { display: grid; gap: 8px; }
.template select { flex: 1; }
#tplParams { display: grid; gap: 6px; }
.note summary { cursor: pointer; color: var(--dim); font-size: 13px; }
.note textarea { width: 100%; margin: 6px 0; }

/* dialogs */
.dialog { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 16px; width: min(640px, 94vw); padding: 22px 24px; }
.dialog::backdrop { background: rgba(0,0,0,0.65); }
.dialog h2 { font-family: 'Cinzel', serif; color: var(--gold-hi); margin: 0 0 10px; }
.dialog h3 { color: var(--gold); margin: 18px 0 6px; font-size: 16px; }
.dialog__close { float: right; }
.status { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; font-size: 14px; }
.status .ok { color: var(--wa); }
.status .no { color: var(--err); }
.switch { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.keyout { border: 1px solid var(--gold); border-radius: 12px; padding: 10px 14px; margin-top: 12px; }
.copy { display: flex; gap: 8px; align-items: center; }
.copy code { flex: 1; }
.stack { display: grid; gap: 10px; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .app.has-thread .side { display: none; }
  .app:not(.has-thread) .thread { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 92%; }
  .pane__top { flex-wrap: wrap; row-gap: 6px; }
  .who { flex: 1 1 calc(100% - 60px); }
  .window { margin-left: 48px; }
}
