:root {
  --bg: #f4f5f7;
  --panel: #fff;
  --text: #1d2330;
  --muted: #6b7280;
  --line: #e2e5ea;
  --accent: #2563eb;
  --in: #fff;
  --out: #dbeafe;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); text-decoration: none; }

h1 { font-size: 1.15rem; margin: 0 0 .75rem; }

button {
  font: inherit;
  padding: .45rem 1rem;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

button:disabled { opacity: .6; cursor: progress; }

button.link { background: none; color: var(--accent); padding: 0; }

input, textarea {
  font: inherit;
  width: 100%;
  padding: .5rem .6rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

textarea { min-height: 5.5rem; resize: vertical; }

.error { color: var(--bad); }
.warn { color: var(--warn); }
.empty, .hint { color: var(--muted); }

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .6rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand { font-weight: 600; color: var(--text); }

.tabs { display: flex; gap: .75rem; }
.tabs a { color: var(--muted); }
.tabs a.current { color: var(--text); font-weight: 600; }

.modem { display: flex; align-items: center; gap: .5rem; color: var(--muted); font-size: .9rem; margin-left: auto; }
.modem .dot { width: .6rem; height: .6rem; border-radius: 50%; background: var(--bad); }
.modem-ready .dot { background: var(--ok); }
.modem-error { color: var(--bad); }
.access {
  font-size: .7rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: .02em;
  border: 1px solid currentColor;
  border-radius: 4px;
  padding: 0 .25rem;
}
.icon-button { display: inline-flex; color: var(--muted); }
.icon-button:hover { color: var(--text); }
button.icon-button { background: none; color: var(--muted); padding: .15rem; }
button.icon-button:hover { color: var(--text); }
.settings { display: flex; flex-direction: column; gap: 1.25rem; max-width: 32rem; }
.settings h2 { font-size: 1rem; margin: 0; }
.storage { display: flex; flex-direction: column; align-items: flex-start; gap: .5rem; }
.storage p { margin: 0; display: flex; justify-content: space-between; gap: 1rem; width: 100%; }
.switch { position: relative; display: flex; align-items: center; gap: .75rem; cursor: pointer; }
.switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
}
.switch-ui {
  position: relative;
  flex: none;
  width: 2.6rem;
  height: 1.5rem;
  background: #d1d5db;
  border-radius: 999px;
}
.switch-ui::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 1.25rem;
  height: 1.25rem;
  background: #fff;
  border-radius: 50%;
  transition: transform .15s ease;
}
.switch input:checked + .switch-ui { background: var(--ok); }
.switch input:checked + .switch-ui::after { transform: translateX(1.05rem); }
.switch input:focus-visible + .switch-ui { outline: 2px solid var(--accent); outline-offset: 2px; }
.bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 1rem; }
.bars i { display: block; width: 4px; background: var(--line); border-radius: 1px; }
.bars i:nth-child(1) { height: 30%; }
.bars i:nth-child(2) { height: 55%; }
.bars i:nth-child(3) { height: 75%; }
.bars i:nth-child(4) { height: 100%; }
.bars-1 i:nth-child(-n+1),
.bars-2 i:nth-child(-n+2),
.bars-3 i:nth-child(-n+3),
.bars-4 i { background: var(--ok); }

.layout { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 20rem;
  flex-shrink: 0;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 1rem;
  overflow-y: auto;
}

.conversations { display: flex; flex-direction: column; }
.conversations .empty { padding: 0 1rem; }

.conv {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: .1rem .5rem;
  padding: .6rem 1rem;
  border-bottom: 1px solid var(--line);
  color: var(--text);
}

.conv:hover, .conv.active { background: var(--bg); }
.conv.new-conv { display: block; color: var(--accent); }
.conv-peer { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.conv-time { color: var(--muted); font-size: .8rem; }
.conv-last { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .conv-last { color: var(--text); }

.badge {
  justify-self: end;
  min-width: 1.4rem;
  padding: 0 .4rem;
  border-radius: 1rem;
  background: var(--accent);
  color: #fff;
  font-size: .8rem;
  text-align: center;
}

.thread-head { display: flex; align-items: center; gap: .75rem; }
.thread-head h1, .thread-id h1 { margin: 0; }
.thread-id { min-width: 0; }
.thread-number { display: flex; align-items: center; gap: .4rem; color: var(--muted); }
.thread-number h1 { color: var(--text); }
.pencil { display: inline-flex; color: var(--muted); }
.pencil:hover { color: var(--accent); }
.back { display: none; font-size: 1.3rem; }

.contact { display: flex; flex-direction: column; gap: .75rem; max-width: 28rem; }
.contact label { display: flex; flex-direction: column; gap: .25rem; }
.contact .back { display: inline; font-size: 1rem; }
.view-contact .sidebar { display: none; }

/* column-reverse keeps the newest message in view as the thread grows. */
.thread-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
  margin: .75rem 0;
}

.thread { display: flex; flex-direction: column; gap: .5rem; }

.msg {
  max-width: 75%;
  padding: .5rem .75rem;
  border-radius: 10px;
  background: var(--in);
  border: 1px solid var(--line);
}

.msg-out { align-self: flex-end; background: var(--out); border-color: transparent; }
.msg-body { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-meta { margin-top: .25rem; color: var(--muted); font-size: .8rem; }
.msg-failed .msg-state { color: var(--bad); }
.msg-delivered .msg-state { color: var(--ok); }

.compose { display: flex; flex-direction: column; gap: .5rem; }
.compose.new { max-width: 36rem; }
.compose label { display: flex; flex-direction: column; gap: .25rem; }
.compose-bar { display: flex; align-items: center; gap: .75rem; }
.compose-bar button { margin-left: auto; }
.counter { color: var(--muted); font-size: .85rem; }

.ussd-form { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; max-width: 36rem; }
.ussd-form input { width: 12rem; }
.ussd-form .error { flex-basis: 100%; margin: 0; }
.htmx-indicator { display: none; color: var(--muted); }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline; }

.ussd-log { display: flex; flex-direction: column; gap: .5rem; margin-top: 1rem; max-width: 36rem; }
.ussd-entry { padding: .5rem .75rem; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.ussd-entry header { display: flex; align-items: center; gap: .5rem; color: var(--muted); font-size: .85rem; }
.ussd-entry header .icon-button { margin-left: auto; }
.ussd-entry pre { margin: .25rem 0 0; white-space: pre-wrap; font: inherit; }
.ussd-entry .error { margin: .25rem 0 0; }

.view-login { align-items: center; justify-content: center; }

.login {
  width: min(22rem, 90vw);
  display: flex;
  flex-direction: column;
  gap: .75rem;
  padding: 1.5rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.login label { display: flex; flex-direction: column; gap: .25rem; }

@media (max-width: 720px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; border-right: 0; }
  .view-index .content { order: -1; }
  .view-thread .sidebar, .view-ussd .sidebar { display: none; }
  .back { display: inline; }
  .msg { max-width: 90%; }
}
