:root {
  color-scheme: light;
  --bg: #eef1f4;
  --panel: #ffffff;
  --ink: #1c2430;
  --muted: #5d6b7a;
  --line: #e2e7ee;
  --accent: #0f6b5c;
  --accent-dark: #0c564a;
  --on-accent: #ffffff;
  --danger: #8d1d16;
  --failed-bg: #8d1d16;
  --warn-bg: #fff6e6;
  --warn-ink: #7a4e00;
  --warn-line: #f0e0c0;
  --danger-bg: #fdecec;
  --danger-line: #f6d0d0;
  --btn-bg: #ffffff;
  --btn-border: #d5dde6;
  --btn-hover: #f6f8fa;
  --hover: #f7f9fb;
  --active: #e7f4f1;
  --menu-hover: #f4f7f6;
  --incoming: #ffffff;
  --incoming-border: #e4e9ef;
  --field-bg: #ffffff;
  --field-border: #d5dde6;
  --code-bg: #f6f8fa;
  --focus: rgba(15, 107, 92, 0.25);
  --overlay: rgba(20, 28, 36, 0.45);
  --toast-bg: #1c2430;
  --toast-ink: #ffffff;
  --shadow: 0 12px 32px rgba(20, 32, 45, 0.12);
  --radius: 12px;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101418;
  --panel: #1a2128;
  --ink: #e8eef3;
  --muted: #9aabba;
  --line: #2d3842;
  --accent: #2a9a84;
  --accent-dark: #217c6b;
  --on-accent: #ffffff;
  --danger: #ffb4ab;
  --failed-bg: #8d2e28;
  --warn-bg: #3a2d14;
  --warn-ink: #f6d7a1;
  --warn-line: #6b5424;
  --danger-bg: #3a2220;
  --danger-line: #6e3934;
  --btn-bg: #24303a;
  --btn-border: #3a4854;
  --btn-hover: #2d3c48;
  --hover: #24303a;
  --active: #17332e;
  --menu-hover: #24303a;
  --incoming: #24303a;
  --incoming-border: #364552;
  --field-bg: #12181e;
  --field-border: #3a4854;
  --code-bg: #12181e;
  --focus: rgba(42, 154, 132, 0.4);
  --overlay: rgba(0, 0, 0, 0.62);
  --toast-bg: #e8eef3;
  --toast-ink: #12181e;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
}

button, input, textarea { font: inherit; color: inherit; }

button { cursor: pointer; }

h1, h2, h3, p { margin: 0; }

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; gap: 12px; align-items: center; }

.brand h1 { font-size: 16px; font-weight: 650; }

.brand p { color: var(--muted); font-size: 12px; margin-top: 2px; }

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  border-radius: 10px;
  padding: 8px 12px;
  color: var(--ink);
}

.btn:hover { background: var(--btn-hover); }

.btn.small { padding: 6px 10px; font-size: 13px; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.btn.primary:hover { background: var(--accent-dark); }

.btn.danger { color: var(--danger); }

.btn:disabled { opacity: 0.55; cursor: default; }

.banner {
  padding: 10px 16px;
  background: var(--warn-bg);
  color: var(--warn-ink);
  border-bottom: 1px solid var(--warn-line);
  font-size: 13px;
}

.banner-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border-bottom-color: var(--danger-line);
}

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
}

.sidebar, .thread {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.search-wrap { padding: 12px; border-bottom: 1px solid var(--line); }

.conversation-list, .messages {
  flex: 1;
  overflow: auto;
  min-height: 0;
}

.conversation {
  width: 100%;
  display: block;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 12px 14px;
}

.conversation:hover { background: var(--hover); }

.conversation.active { background: var(--active); }

.conversation-top, .conversation-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.conversation-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation.unread .conversation-name,
.conversation.unread .preview { font-weight: 700; }

.time, .preview, .subnumber, .hint, .optional, #char-count {
  color: var(--muted);
  font-size: 12px;
}

.subnumber { margin-top: 2px; }

.preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 4px;
  flex: 1;
}

.conversation.unread .preview { color: var(--ink); }

.badge {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.empty-list, .menu-empty {
  color: var(--muted);
  padding: 16px;
}

.thread { background: var(--bg); }

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 24px;
}

.empty-state h2 {
  color: var(--ink);
  font-size: 20px;
  font-weight: 650;
  margin-bottom: 6px;
}

.thread-view {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.thread-header {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.thread-person { flex: 1; min-width: 0; }

.name-row { display: flex; align-items: center; gap: 8px; }

#contact-name {
  width: min(420px, 100%);
  border: 1px solid transparent;
  background: transparent;
  font-size: 16px;
  font-weight: 650;
  padding: 4px 6px;
  border-radius: 8px;
}

#contact-name:hover,
#contact-name:focus {
  border-color: var(--field-border);
  background: var(--field-bg);
  outline: none;
}

.phone { padding-left: 6px; margin-top: 2px; }

.saved { color: var(--accent); font-size: 12px; }

.messages { padding: 8px 0 16px; }

.day-sep {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  margin: 14px 0 8px;
}

.bubble-row { display: flex; margin: 4px 16px; }

.bubble-row.in { justify-content: flex-start; }

.bubble-row.out { justify-content: flex-end; }

.bubble {
  max-width: min(68%, 560px);
  padding: 10px 12px;
  border-radius: 14px;
  line-height: 1.4;
}

.bubble-row.in .bubble {
  background: var(--incoming);
  border: 1px solid var(--incoming-border);
  border-bottom-left-radius: 4px;
}

.bubble-row.out .bubble {
  background: var(--accent);
  color: var(--on-accent);
  border-bottom-right-radius: 4px;
}

.bubble-row.out.failed .bubble { background: var(--failed-bg); color: #fff; }

.bubble-text { white-space: pre-wrap; overflow-wrap: anywhere; }

.media-note { font-size: 12px; margin-top: 4px; opacity: 0.85; }

.meta { font-size: 11px; margin-top: 4px; opacity: 0.75; }

.composer {
  padding: 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.composer-main { display: flex; gap: 8px; align-items: flex-end; }

.composer-main textarea { flex: 1; min-height: 52px; }

.send-btn { height: 42px; min-width: 76px; }

.composer-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.template-picker { position: relative; }

.menu {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  width: min(320px, 70vw);
  max-height: 280px;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 5;
  padding: 6px;
}

.menu-item, .menu-manage {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 8px;
  padding: 8px;
}

.menu-item strong, .menu-item small { display: block; }

.menu-item small {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.menu-item:hover, .menu-manage:hover { background: var(--menu-hover); }

.menu-manage {
  color: var(--accent);
  font-weight: 600;
  border-top: 1px solid var(--line);
  border-radius: 0;
  margin-top: 4px;
}

.back-btn { display: none; }

input[type="text"],
input[type="search"],
textarea {
  width: 100%;
  border: 1px solid var(--field-border);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--field-bg);
}

textarea { resize: vertical; }

input:focus, textarea:focus {
  outline: 2px solid var(--focus);
  border-color: var(--accent);
}

.form-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--overlay);
}

.modal-card {
  position: relative;
  width: min(520px, 100%);
  background: var(--panel);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 20px;
  max-height: calc(100vh - 40px);
  overflow: auto;
}

.modal-wide { width: min(680px, 100%); }

.modal-card h2 { font-size: 18px; margin-bottom: 8px; }

.modal-card h3 { font-size: 15px; margin: 18px 0 8px; }

label {
  display: block;
  font-size: 13px;
  font-weight: 650;
  margin: 12px 0 6px;
}

.optional { font-weight: 500; }

.hint { margin-top: 6px; line-height: 1.4; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.template-admin-list { margin-top: 18px; display: flex; flex-direction: column; gap: 8px; }

.template-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
}

.template-row p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.template-actions { display: flex; gap: 6px; align-items: flex-start; }

.settings-grid {
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 10px 12px;
  align-items: start;
  margin-top: 12px;
}

.settings-grid dt { color: var(--muted); }

.settings-grid dd { margin: 0; }

.url-row { display: flex; gap: 8px; align-items: flex-start; }

.url-row code {
  flex: 1;
  word-break: break-all;
  font-size: 13px;
  background: var(--code-bg);
  border-radius: 8px;
  padding: 8px;
}

.instructions { margin: 0; padding-left: 18px; }

.instructions li { margin: 6px 0; }

.instructions a { color: var(--accent); }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--toast-ink);
  padding: 10px 14px;
  border-radius: 999px;
  z-index: 30;
  font-size: 13px;
  max-width: min(480px, calc(100% - 32px));
}

.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .back-btn { display: inline-flex; }
  #thread.is-hidden-mobile,
  #sidebar.is-hidden-mobile { display: none; }
  .bubble { max-width: 85%; }
  .settings-grid { grid-template-columns: 1fr; }
  .template-row { flex-direction: column; }
}

@media (max-width: 640px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .top-actions { width: 100%; }
  .top-actions .btn { flex: 1; }
}
