:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #d9dde3;
  --text: #1c2430;
  --muted: #667085;
  --accent: #1f6feb;
  --accent-soft: #e8f1ff;
  --inbound: #eef1f5;
  --outbound: #d7e8ff;
  --danger: #b42318;
  --unread: #1f6feb;
  --sidebar-width: 340px;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
}
html {
  height: 100%;
  height: 100dvh;
}
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
}
/* Inbox shell must not grow the document; panes scroll internally. */
body.app-shell {
  overflow: hidden;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }
.error { color: var(--danger); font-size: 13px; margin: 0 0 8px; }

button, .file-btn {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  font: inherit;
}
button.secondary, .linkish {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}
button:disabled { opacity: 0.6; cursor: default; }

/* Login */
.login-page {
  display: grid;
  place-items: center;
  min-height: 100%;
  background:
    radial-gradient(circle at top left, #dbe8ff, transparent 40%),
    linear-gradient(180deg, #eef2f7, #f7f8fa);
}
.login-card {
  width: min(380px, 92vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  box-shadow: 0 10px 30px rgba(16, 24, 40, 0.06);
}
.login-card h1 { margin: 0 0 4px; font-size: 24px; }
.login-card label { display: grid; gap: 6px; margin: 14px 0; font-size: 13px; }
.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}
.login-card button { width: 100%; margin-top: 8px; padding: 10px; }

/* Shell */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  height: 100vh;
  height: 100dvh;
  max-height: 100vh;
  max-height: 100dvh;
  overflow: hidden;
}
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.sidebar-header {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.notify-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
}
.notify-btn {
  padding: 4px 8px;
  font-size: 12px;
  line-height: 1.2;
}
.notify-sound {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.notify-sound input {
  margin: 0;
}
.notify-status {
  flex: 1 1 100%;
  margin: 0;
}
.search-box {
  position: relative;
  flex: 0 0 auto;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-actions {
  flex: 0 0 auto;
  padding: 10px 12px 0;
}
.new-message-btn {
  width: 100%;
  padding: 8px 12px;
  font-size: 13px;
}
.new-message-to {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--muted);
}
.new-message-to input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  color: var(--text);
}
.search-box input {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}
.search-results {
  position: absolute;
  left: 12px; right: 12px; top: 46px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 320px;
  overflow: auto;
  z-index: 5;
  box-shadow: 0 8px 24px rgba(0,0,0,.08);
}
.search-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  color: inherit;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  padding: 10px 12px;
}
.search-item:hover { background: var(--accent-soft); }

.conversation-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.papertrail-box {
  flex: 0 0 auto;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.papertrail-title {
  font-size: 12px;
  font-weight: 600;
  margin: 0 0 6px;
}
.papertrail-box textarea {
  width: 100%;
  resize: vertical;
  min-height: 64px;
  max-height: 120px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
}
.papertrail-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.papertrail-actions button {
  padding: 6px 10px;
  font-size: 12px;
}
.papertrail-status-ok { color: #157347; }
.papertrail-status-err { color: var(--danger); }
.conversation-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 10px;
  width: 100%;
  text-align: left;
  background: transparent;
  color: inherit;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  padding: 12px 14px;
}
.conversation-item:hover { background: #f8fafc; }
.conversation-item.active { background: var(--accent-soft); }
.conversation-item .title { font-weight: 600; font-size: 14px; }
.conversation-item .preview {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conversation-item .meta { font-size: 11px; color: var(--muted); }
.badge {
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--unread);
  color: #fff;
  font-size: 11px;
  display: inline-grid;
  place-items: center;
}

.thread-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background:
    linear-gradient(180deg, #f7f9fc 0%, #eef2f7 100%);
}
.thread-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  min-height: 0;
}
.thread-heading { min-width: 0; flex: 1 1 auto; }
.thread-header h1 { margin: 0; font-size: 18px; }
.thread-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  flex: 0 0 auto;
}
.mobile-back { display: none; }

.message-list {
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.bubble-row {
  display: flex;
  flex: 0 0 auto;
}
.bubble-row.inbound { justify-content: flex-start; }
.bubble-row.outbound { justify-content: flex-end; }
.bubble {
  max-width: min(640px, 78%);
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--inbound);
  box-shadow: 0 1px 0 rgba(0,0,0,.03);
  overflow: hidden;
}
.bubble-row.outbound .bubble { background: var(--outbound); }
.bubble .body { white-space: pre-wrap; word-break: break-word; font-size: 14px; line-height: 1.4; }
.bubble .time { margin-top: 6px; font-size: 11px; color: var(--muted); }
.bubble .attachments { margin-top: 8px; display: grid; gap: 8px; }
.bubble .attachments img {
  display: block;
  width: auto;
  height: auto;
  max-width: min(100%, 420px);
  max-height: 280px;
  object-fit: contain;
  border-radius: 8px;
}
.bubble .file-link {
  display: inline-block;
  padding: 8px 10px;
  background: rgba(255,255,255,.65);
  border-radius: 8px;
  color: var(--accent);
  text-decoration: none;
  font-size: 13px;
}
.unavailable { font-size: 12px; color: var(--muted); font-style: italic; }

.shortcode-notice {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  font-size: 13px;
  color: var(--muted);
}

.composer {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  min-height: 0;
}
.composer textarea {
  width: 100%;
  resize: vertical;
  min-height: 54px;
  max-height: 160px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
}
.composer-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}
.compose-count {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}
.file-btn { display: inline-block; }
.file-btn input { display: none; }

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  width: min(420px, 92vw);
}
dialog::backdrop { background: rgba(16,24,40,.35); }
#contactForm { padding: 18px; display: grid; gap: 12px; }
#contactForm h2 { margin: 0; font-size: 18px; }
#contactForm label { display: grid; gap: 6px; font-size: 13px; }
#contactForm input, #contactForm textarea {
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
  }
  .sidebar {
    display: none;
    min-height: 0;
    border-right: 0;
  }
  .thread-pane { min-height: 0; }
  .app-shell.show-list .sidebar { display: flex; }
  .app-shell.show-list .thread-pane { display: none; }
  .papertrail-box { display: none !important; }

  .mobile-back {
    display: inline-block;
    flex: 0 0 auto;
    padding: 8px 10px;
    font-size: 14px;
    white-space: nowrap;
  }
  .sidebar-header {
    padding-top: max(14px, env(safe-area-inset-top, 0px));
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }
  .sidebar-actions {
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
  }
  .search-box {
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
  }
  .thread-header {
    padding-top: max(12px, env(safe-area-inset-top, 0px));
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
    gap: 8px;
  }
  .thread-header h1 {
    font-size: 16px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .composer {
    padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
  }
  .composer textarea,
  .search-box input,
  .new-message-to input {
    font-size: 16px;
  }
  .bubble {
    max-width: min(640px, 86%);
  }
  .bubble .attachments img {
    max-width: 100%;
  }
  .conversation-item {
    padding-left: max(14px, env(safe-area-inset-left, 0px));
    padding-right: max(14px, env(safe-area-inset-right, 0px));
  }
}
