:root {
  --bg: #ffffff;
  --bg-alt: #f5f6f8;
  --bg-hover: #ebedf1;
  --border: #dcdfe5;
  --text: #14161a;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --bubble-in: #eceef2;
  --bubble-out: #2563eb;
  --danger: #c2334d;
  --ok: #16a34a;
  --radius: 12px;
  --sidebar-w: 320px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --bg-alt: #1b1e24;
    --bg-hover: #262a32;
    --border: #2c313a;
    --text: #e8eaed;
    --text-muted: #9aa1ac;
    --accent: #3b82f6;
    --bubble-in: #262a32;
    --bubble-out: #2563eb;
    --danger: #f2708b;
    --ok: #4ade80;
  }
}

* {
  box-sizing: border-box;
}

/* Author `display` rules below outrank the UA stylesheet's [hidden] rule, so
   the attribute needs to win explicitly or hidden panels still render. */
[hidden] {
  display: none !important;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1 {
  font-size: 19px;
  margin: 0;
}
h2 {
  font-size: 16px;
  margin: 0;
}

.muted {
  color: var(--text-muted);
}
.small {
  font-size: 12px;
}

.error {
  color: var(--danger);
  font-size: 13px;
  margin: 6px 0 0;
  min-height: 1em;
}

button {
  font: inherit;
  cursor: pointer;
}

/* --- Login ---------------------------------------------------------------- */

.login {
  display: grid;
  place-items: center;
  height: 100%;
  padding: 20px;
}

.login-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 28px;
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.login-card input,
#new-number {
  padding: 11px 13px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
}

.login-card input:focus-visible,
#new-number:focus-visible,
#body:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.login-card button,
#new-form button[type='submit'] {
  padding: 11px 13px;
  color: var(--accent-text);
  background: var(--accent);
  border: none;
  border-radius: 9px;
  font-weight: 550;
}

/* --- Layout --------------------------------------------------------------- */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100%;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-alt);
  border-right: 1px solid var(--border);
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
}

.icon-btn {
  width: 34px;
  height: 34px;
  font-size: 17px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 9px;
}
.icon-btn:hover {
  background: var(--bg-hover);
}

.link-btn {
  color: var(--text-muted);
  background: none;
  border: none;
  font-size: 13px;
  text-decoration: underline;
  padding: 0;
}

.conn {
  font-size: 11px;
  color: var(--text-muted);
}
.conn.live {
  color: var(--ok);
}
.conn.down {
  color: var(--danger);
}

/* --- Conversation list ---------------------------------------------------- */

.conversations {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.conv {
  display: block;
  width: 100%;
  text-align: left;
  padding: 12px 16px;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.conv:hover {
  background: var(--bg-hover);
}
.conv.active {
  background: var(--bg-hover);
  box-shadow: inset 3px 0 0 var(--accent);
}

.conv-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.conv-name {
  font-weight: 550;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  flex: none;
  font-size: 11px;
  color: var(--text-muted);
}

.conv-preview {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 3px;
  font-size: 13px;
  color: var(--text-muted);
}

.conv-preview span:first-child {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  flex: none;
  min-width: 19px;
  height: 19px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 11px;
  font-weight: 600;
  line-height: 19px;
  text-align: center;
}

/* --- Thread --------------------------------------------------------------- */

.thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.empty {
  display: grid;
  place-items: center;
  height: 100%;
  color: var(--text-muted);
}

.thread-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.thread-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.back {
  display: none;
}

.messages {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.load-more {
  align-self: center;
  margin-bottom: 12px;
  padding: 6px 14px;
  font-size: 13px;
  color: var(--text-muted);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 20px;
}

.day {
  align-self: center;
  margin: 14px 0 8px;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.msg {
  max-width: min(60ch, 78%);
  padding: 8px 13px;
  border-radius: 17px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.in {
  align-self: flex-start;
  background: var(--bubble-in);
  border-bottom-left-radius: 5px;
}

.msg.out {
  align-self: flex-end;
  background: var(--bubble-out);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.msg.failed {
  background: var(--danger);
  color: #fff;
}

.msg-meta {
  align-self: flex-end;
  margin: 1px 2px 8px;
  font-size: 11px;
  color: var(--text-muted);
}
.msg-meta.in {
  align-self: flex-start;
}
.msg-meta.failed {
  color: var(--danger);
}

.media-note {
  display: block;
  margin-top: 5px;
  font-size: 12px;
  opacity: 0.8;
}

/* --- Composer ------------------------------------------------------------- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}

#body {
  flex: 1;
  resize: none;
  min-height: 40px;
  max-height: 160px;
  padding: 10px 14px;
  font: inherit;
  color: var(--text);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 19px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.composer-side {
  display: flex;
  align-items: center;
  gap: 10px;
}

.segments {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}
.segments.warn {
  color: var(--danger);
}

#send-btn {
  padding: 10px 18px;
  color: var(--accent-text);
  background: var(--accent);
  border: none;
  border-radius: 19px;
  font-weight: 550;
}
#send-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.thread-error {
  padding: 0 18px 10px;
}

/* --- Dialog --------------------------------------------------------------- */

dialog {
  padding: 22px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: min(340px, 90vw);
}
dialog::backdrop {
  background: rgb(0 0 0 / 45%);
}

#new-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#new-form menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 6px 0 0;
  padding: 0;
}

#new-cancel {
  padding: 10px 14px;
  color: var(--text);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 9px;
}

/* --- Mobile --------------------------------------------------------------- */

@media (max-width: 720px) {
  .app {
    grid-template-columns: 1fr;
  }
  .sidebar {
    border-right: none;
  }
  .app.viewing-thread .sidebar {
    display: none;
  }
  .app:not(.viewing-thread) .thread {
    display: none;
  }
  .back {
    display: block;
  }
  .msg {
    max-width: 85%;
  }
}
