:root {
  --bg: #0e1311;
  --surface: #151c19;
  --surface-2: #1b2521;
  --border: #26332d;
  --text: #e4ebe7;
  --muted: #9cada4;
  --accent: #6fdb94;
  --accent-strong: #1f6e3d;
  --gold: #e6c47e;
  --out: #1d4a31;
  --error: #ff8f8f;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }

code {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.92em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.05em 0.35em;
}

button, input, select, textarea { font: inherit; color: inherit; }

button {
  background: var(--accent-strong);
  border: 1px solid #2e9a57;
  border-radius: 8px;
  color: #fff;
  font-weight: 600;
  padding: 9px 16px;
  cursor: pointer;
}
button:hover, button:focus-visible { background: #268549; }
button:disabled { opacity: 0.6; cursor: default; }

.link-button { background: none; border: none; color: var(--accent); padding: 6px 4px; font-weight: 500; }
.link-button:hover, .link-button:focus-visible { background: none; text-decoration: underline; }

input, select, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #2e9a57; outline-offset: 0; }

.muted { color: var(--muted); }
.small { font-size: 0.88rem; }
.center { text-align: center; }
.error { color: var(--error); }

/* ---- top bar ---------------------------------------------------------------------------- */

.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; margin-right: auto; white-space: nowrap; }
.brand img { display: block; }
#character { max-width: 46vw; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ---- link view -------------------------------------------------------------------------- */

.card {
  max-width: 460px;
  width: calc(100% - 32px);
  margin: 32px auto;
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.card h1 { margin: 0 0 12px; font-size: 1.4rem; color: var(--gold); }
.card ol { padding-left: 1.2em; }
.card li { margin-bottom: 8px; }
#link-form { display: flex; gap: 8px; margin: 14px 0 6px; }
#code { flex: 1; font-size: 1.3rem; letter-spacing: 0.12em; text-transform: uppercase; text-align: center; }

/* ---- messages app ----------------------------------------------------------------------- */

#app { flex: 1; min-height: 0; display: flex; }

#list-pane {
  width: 100%;
  overflow-y: auto;
  padding: 12px 16px 24px;
}
#new { width: 100%; margin-bottom: 12px; }

#conversations { list-style: none; margin: 0; padding: 0; }
#conversations li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 10px;
  padding: 11px 12px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
}
#conversations li:hover { background: var(--surface); }
#conversations li.selected { background: var(--surface-2); border-color: var(--border); }
#conversations .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#conversations .when { color: var(--muted); font-size: 0.82rem; }
#conversations .snippet { color: var(--muted); font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#conversations .badge {
  justify-self: end;
  min-width: 1.6em;
  padding: 0 0.45em;
  border-radius: 999px;
  background: var(--accent);
  color: #062010;
  font-size: 0.8rem;
  font-weight: 700;
  text-align: center;
}
#conversations li.unread .who { color: var(--accent); }

#chat-pane { display: none; flex: 1; min-width: 0; flex-direction: column; }

.chat-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 48px;
}
#chat-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#to-row { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
#to-row input { flex: 1; }

#messages { flex: 1; min-height: 0; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; }

.msg { max-width: 80%; padding: 8px 12px; border-radius: 14px; background: var(--surface-2); align-self: flex-start; overflow-wrap: anywhere; }
.msg.out { background: var(--out); align-self: flex-end; }
.msg .time { display: block; margin-top: 2px; font-size: 0.75rem; color: var(--muted); }
.day { align-self: center; color: var(--muted); font-size: 0.8rem; margin: 6px 0; }

#pick { margin: auto 16px; }

#compose {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 16px 4px;
  border-top: 1px solid var(--border);
}
#text { flex: 1; resize: none; max-height: 9em; overflow-y: hidden; }

.compose-foot {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 2px 16px 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  min-height: 1.6em;
}

/* phones: one pane at a time */
body.chatting #list-pane { display: none; }
body.chatting #chat-pane { display: flex; }

/* wider screens: both panes, no back button */
@media (min-width: 760px) {
  #list-pane { width: 320px; flex: none; border-right: 1px solid var(--border); }
  #chat-pane, body.chatting #chat-pane { display: flex; }
  body.chatting #list-pane { display: block; }
  #back { display: none; }
}
