:root {
  --navy: #17324d;
  --blue: #1768b5;
  --light-blue: #e7f3fc;
  --ink: #18212b;
  --muted: #66717c;
  --line: #dce3e9;
  --surface: #ffffff;
  --background: #f2f5f7;
  --danger: #a72828;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; background: var(--background); }
body {
  min-height: 100vh;
  min-height: 100dvh;
}
a { color: var(--blue); }
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.topbar { height: 64px; padding: 0 max(18px, env(safe-area-inset-left)); display: flex; align-items: center; justify-content: space-between; background: var(--navy); color: white; box-shadow: 0 2px 12px #10283c33; }
.brand { display: flex; align-items: center; gap: 10px; color: white; font-weight: 750; text-decoration: none; }
.brand-mark { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; color: var(--navy); background: white; font-size: 13px; }
.top-unread-count { display: grid; min-width: 22px; height: 22px; padding: 0 6px; place-items: center; border-radius: 11px; color: var(--navy); background: #ffd34e; font-size: 12px; font-weight: 850; }
.top-actions { display: flex; align-items: center; gap: 7px; }
.top-actions form { margin: 0; }
.icon-button, .text-button { border: 0; color: white; background: transparent; text-decoration: none; cursor: pointer; }
.icon-button { display: grid; place-items: center; min-width: 38px; min-height: 38px; font-size: 20px; }
.text-button { padding: 9px; }
.page-shell { width: min(1040px, 100%); margin: 0 auto; padding: 24px; }

.flash-stack { position: fixed; z-index: 20; top: 75px; left: 50%; transform: translateX(-50%); width: min(640px, calc(100% - 32px)); display: grid; gap: 8px; }
.flash { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 12px 12px 12px 16px; border-radius: 9px; background: #fff4cf; border: 1px solid #f1d77c; box-shadow: 0 8px 25px #0002; transition: opacity .2s ease, transform .2s ease; }
.flash.success { background: #e7f7ed; border-color: #9dd7ad; }
.flash.error { background: #fff0ef; border-color: #e9aaa5; }
.flash.is-hiding { opacity: 0; transform: translateY(-6px); }
.flash-close { flex: 0 0 32px; width: 32px; height: 32px; padding: 0; border: 0; border-radius: 50%; color: inherit; background: transparent; cursor: pointer; font-size: 22px; line-height: 1; }
.flash-close:hover { background: #0000000c; }

.inbox-panel, .thread-panel, .form-card, .auth-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 5px 22px #17324d0b; overflow: hidden; }
.section-header { display: flex; justify-content: space-between; gap: 18px; align-items: center; padding: 22px 24px 12px; }
.section-actions { display: flex; align-items: center; gap: 9px; }
.section-actions form { margin: 0; }
h1 { margin: 0 0 5px; font-size: 1.6rem; }
.inbox-heading { display: flex; align-items: center; gap: 9px; }
.heading-unread-count { padding: 4px 8px; border-radius: 12px; color: white; background: var(--blue); font-size: 11px; font-weight: 800; }
h2 { margin-top: 0; }
.muted { color: var(--muted); margin: 0; }
.primary-button, .send-button { border: 0; border-radius: 9px; background: var(--blue); color: white; padding: 11px 16px; font-weight: 700; text-decoration: none; cursor: pointer; }
.call-button { border: 0; border-radius: 9px; background: #18844a; color: white; padding: 11px 16px; font-weight: 700; text-decoration: none; cursor: pointer; }
.call-button:hover { background: #116c3b; }
.secondary-button { border: 1px solid #9fb3c4; border-radius: 9px; background: white; color: var(--navy); padding: 10px 14px; font-weight: 700; text-decoration: none; cursor: pointer; }
.secondary-button:hover { background: var(--light-blue); }
.primary-button:hover, .send-button:hover { background: #105798; }
button:disabled { opacity: .5; cursor: not-allowed; }

.search-row { display: flex; gap: 8px; padding: 10px 24px; }
.sync-status { margin: 0 24px 5px; padding: 8px 11px; border-radius: 7px; color: #49616f; background: #edf5fa; font-size: 12px; }
.sync-status.failed { color: var(--danger); background: #fff0ef; }
.search-row input { flex: 1; min-width: 0; }
.search-row button { border: 1px solid var(--line); border-radius: 8px; padding: 9px 13px; background: white; }
input, select, textarea { width: 100%; border: 1px solid #bfcbd4; border-radius: 8px; padding: 11px 12px; color: var(--ink); background: white; }
input:focus, select:focus, textarea:focus { outline: 3px solid #1768b522; border-color: var(--blue); }
.inbox-tabs { display: flex; gap: 22px; padding: 6px 24px 0; border-bottom: 1px solid var(--line); }
.inbox-tabs a { padding: 10px 1px; color: var(--muted); text-decoration: none; border-bottom: 3px solid transparent; }
.inbox-tabs a.active { color: var(--blue); border-color: var(--blue); font-weight: 700; }
.tab-unread-count { display: inline-grid; min-width: 20px; height: 20px; margin-left: 6px; padding: 0 6px; place-items: center; border-radius: 10px; color: white; background: var(--blue); font-size: 11px; font-weight: 800; }
.conversation-list { min-height: 240px; }
.conversation-row { display: flex; gap: 13px; align-items: center; min-height: 76px; padding: 12px 24px; color: var(--ink); text-decoration: none; border-bottom: 1px solid #edf1f4; }
.conversation-row:hover { background: #f8fbfd; }
.conversation-row.scheduled { background: #fffbeb; box-shadow: inset 4px 0 #d39400; }
.conversation-row.scheduled:hover { background: #fff6d7; }
.conversation-row.unread { background: #eef7ff; box-shadow: inset 4px 0 var(--blue); }
.conversation-row.unread:hover { background: #e5f2fc; }
.conversation-row.unread .avatar { position: relative; color: white; background: var(--blue); }
.conversation-row.unread .avatar::after { position: absolute; top: -2px; right: -2px; width: 10px; height: 10px; content: ""; border: 2px solid white; border-radius: 50%; background: #29a45c; }
.avatar { flex: 0 0 43px; height: 43px; display: grid; place-items: center; border-radius: 50%; background: var(--light-blue); color: var(--blue); font-weight: 800; }
.conversation-summary { min-width: 0; flex: 1; display: grid; gap: 4px; }
.conversation-summary strong, .conversation-summary small, .conversation-preview { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversation-preview { color: #3e4b57; font-size: 14px; }
.conversation-preview.outbound:not(.scheduled)::before { content: "You: "; color: var(--muted); }
.conversation-preview.scheduled::before { content: "Scheduled: "; color: #936600; font-weight: 800; }
.conversation-row.unread .conversation-summary strong, .conversation-row.unread .conversation-preview, .conversation-row.unread .conversation-meta time { color: #102d48; font-weight: 800; }
.conversation-summary small { color: var(--muted); }
.conversation-meta { display: grid; justify-items: end; gap: 7px; color: var(--muted); font-size: 12px; }
.scheduled-time { display: grid; justify-items: end; gap: 4px; white-space: nowrap; }
.scheduled-badge { padding: 3px 7px; border-radius: 9px; color: #704d00; background: #ffe49a; font-size: 10px; font-weight: 850; text-transform: uppercase; letter-spacing: .03em; }
.unread-badge { display: grid; min-width: 22px; height: 22px; padding: 0 6px; place-items: center; border-radius: 11px; color: white; background: var(--blue); font-weight: 800; }
.empty-state { padding: 56px 24px; color: var(--muted); text-align: center; }
.empty-state.compact { min-height: 0; padding: 22px 24px; }
.group-mode-heading { padding: 16px 24px 9px; border-top: 1px solid var(--line); background: #f8fafb; }
.group-mode-heading h2 { margin: 0; font-size: 17px; }
.group-mode-heading p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.broadcast-heading { margin-top: 12px; }

.thread-panel {
  height: calc(100vh - 112px);
  height: calc(100dvh - 112px);
  height: calc(var(--usable-viewport-height, 100dvh) - 112px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}
.thread-header { min-height: 72px; display: flex; gap: 13px; align-items: center; padding: 12px 18px; border-bottom: 1px solid var(--line); }
.thread-back-target { min-width: 0; min-height: 48px; flex: 1; display: flex; gap: 13px; align-items: center; color: var(--ink); text-decoration: none; border-radius: 8px; }
.thread-back-target:hover { background: #f4f8fb; }
.back-link { flex: 0 0 auto; color: var(--ink); font-size: 36px; line-height: 1; }
.thread-person { min-width: 0; flex: 1; }
.thread-person h1 { font-size: 1.1rem; }
.thread-contact-line { display: block; margin: 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.voice-call-state { display: inline-block; margin-top: 5px; padding: 3px 8px; border-radius: 10px; color: #28506b; background: #e8f3fa; font-size: 11px; font-weight: 750; }
.voice-call-state.calling_user, .voice-call-state.awaiting_confirmation, .voice-call-state.connecting, .voice-call-state.ringing_destination { color: #704d00; background: #fff0bd; }
.voice-call-state.in_progress, .voice-call-state.completed { color: #176136; background: #e1f5e8; }
.voice-call-state.failed, .voice-call-state.busy, .voice-call-state.no_answer, .voice-call-state.canceled { color: var(--danger); background: #fff0ef; }
.thread-tools { display: flex; gap: 10px; align-items: center; font-size: 14px; }
.thread-tools form { margin: 0; }
.voice-call-button { min-height: 36px; padding: 7px 11px; border: 0; border-radius: 9px; color: white; background: #18844a; cursor: pointer; font-weight: 800; }
.voice-call-button:hover { background: #116c3b; }
.voice-call-button:disabled { background: #81948a; }
.link-button { border: 0; padding: 0; color: var(--blue); background: transparent; cursor: pointer; }
.danger-link { color: var(--danger); }
.status-warning { display: inline-block; margin-top: 5px; padding: 3px 7px; color: var(--danger); background: #fff0ef; border-radius: 5px; font-size: 12px; font-weight: 700; }
.message-thread { overflow-y: auto; padding: 22px; background: linear-gradient(#f7f9fb, #f2f5f7); }
.message { display: flex; margin: 8px 0; }
.message.inbound { justify-content: flex-start; }
.message.outbound { justify-content: flex-end; }
.bubble { max-width: min(74%, 650px); border-radius: 16px; padding: 10px 13px 7px; box-shadow: 0 1px 2px #0001; overflow-wrap: anywhere; }
.inbound .bubble { background: white; border-bottom-left-radius: 4px; }
.outbound .bubble { color: #102840; background: #dceeff; border-bottom-right-radius: 4px; }
.message-body a { color: inherit; }
.group-message-author { display: block; margin-bottom: 4px; color: var(--blue); font-size: 12px; }
.outbound .group-message-author { color: #315f84; }
.bubble footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; color: #607080; font-size: 10px; }
.delivery.failed, .delivery.undelivered, .message-error { color: var(--danger); }
.message-error { margin-top: 5px; font-size: 11px; }
.message-image { display: block; max-width: 100%; max-height: 360px; border-radius: 10px; margin-top: 5px; }
.attachment-link { display: block; margin: 6px 0; color: inherit; }
.composer { position: relative; display: grid; grid-template-columns: auto 1fr auto; align-items: end; gap: 9px; padding: 12px 14px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: white; }
.composer textarea { resize: none; min-height: 45px; max-height: 120px; }
.attach-button { display: grid; place-items: center; width: 42px; height: 42px; margin-bottom: 2px; border-radius: 50%; color: var(--blue); background: var(--light-blue); cursor: pointer; font-size: 24px; }
.attach-button > span { display: block; line-height: 1; transform: translateY(-1px); }
.attach-button input { display: none; }
.schedule-button, .emoji-button { display: grid; place-items: center; width: 42px; height: 42px; margin-bottom: 2px; padding: 0; border: 0; border-radius: 50%; color: var(--blue); background: var(--light-blue); cursor: pointer; font-size: 24px; line-height: 1; }
.schedule-button > span, .emoji-button > span { display: block; line-height: 1; transform: translateY(-1px); }
.schedule-button.active { color: white; background: var(--blue); }
.composer-field { min-width: 0; display: grid; gap: 6px; }
.schedule-menu { position: absolute; z-index: 20; left: 58px; bottom: calc(100% - 5px); width: min(330px, calc(100% - 76px)); padding: 13px; border: 1px solid var(--line); border-radius: 12px; background: white; box-shadow: 0 8px 28px #1028402b; }
.schedule-menu strong { display: block; margin-bottom: 9px; }
.schedule-presets { display: grid; gap: 6px; }
.schedule-presets button, .schedule-custom button { border: 0; border-radius: 8px; padding: 9px 10px; color: var(--blue); background: var(--light-blue); cursor: pointer; text-align: left; }
.schedule-custom { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 7px; margin-top: 9px; }
.schedule-custom input { min-width: 0; padding: 8px; font-size: 13px; }
.schedule-custom button { text-align: center; font-weight: 700; }
.emoji-menu { position: absolute; z-index: 21; left: 58px; bottom: calc(100% - 5px); width: min(330px, calc(100% - 76px)); max-height: 230px; overflow-y: auto; display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; padding: 9px; border: 1px solid var(--line); border-radius: 12px; background: white; box-shadow: 0 8px 28px #1028402b; }
.emoji-menu[hidden] { display: none; }
.emoji-menu button { display: grid; place-items: center; min-width: 0; height: 40px; padding: 0; border: 0; border-radius: 8px; background: transparent; cursor: pointer; font-size: 23px; }
.emoji-menu button:hover, .emoji-menu button:focus-visible { background: var(--light-blue); outline: none; }
.schedule-chip { width: fit-content; max-width: 100%; display: inline-flex; gap: 8px; align-items: center; padding: 4px 8px; border-radius: 12px; color: var(--blue); background: var(--light-blue); font-size: 11px; }
.schedule-chip[hidden] { display: none; }
.cancel-scheduled { margin-top: 6px; text-align: right; }
.attachment-name { min-width: 0; color: var(--muted); font-size: 11px; line-height: 1.25; overflow-wrap: anywhere; }
.attachment-name:empty { display: none; }
.send-button { height: 45px; }

.form-card, .auth-card { max-width: 640px; margin: 36px auto; padding: 28px; overflow: visible; }
.auth-card { max-width: 420px; }
.stacked-form { display: grid; gap: 10px; margin-top: 22px; }
.stacked-form label { margin-top: 6px; font-weight: 700; }
.stacked-form ul.errorlist { color: var(--danger); }
.field-error { color: var(--danger); }
.back-text { display: inline-block; margin-bottom: 18px; text-decoration: none; }
.group-compose-card, .group-detail-card { max-width: 760px; }
.recipient-heading, .group-detail-heading { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.recipient-heading strong { color: var(--blue); font-size: 13px; }
.recipient-list { max-height: 330px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; background: #fbfcfd; }
.recipient-choice { display: flex; align-items: center; gap: 11px; margin: 0 !important; padding: 10px 12px; border-bottom: 1px solid #edf1f4; cursor: pointer; font-weight: 500 !important; }
.recipient-choice:last-child { border-bottom: 0; }
.recipient-choice:hover { background: var(--light-blue); }
.recipient-choice input { width: 20px; height: 20px; margin: 0; accent-color: var(--blue); }
.group-message-copy { margin: 18px 0; padding: 16px; border-radius: 10px; background: var(--light-blue); white-space: normal; }
.schedule-summary { color: #704d00; font-weight: 750; }
.group-recipient-statuses { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.recipient-status-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 11px 13px; color: var(--ink); text-decoration: none; border-bottom: 1px solid #edf1f4; }
.recipient-status-row:last-child { border-bottom: 0; }
.recipient-status-row:hover { background: #f8fbfd; }
.recipient-status-row > span:first-child { min-width: 0; display: grid; gap: 3px; }
.recipient-status-row small { color: var(--muted); }
.recipient-status-row .delivery { flex: 0 0 auto; font-size: 12px; font-weight: 750; }
.danger-button { border: 1px solid #d79898; border-radius: 9px; padding: 10px 13px; color: var(--danger); background: white; cursor: pointer; font-weight: 750; }
.group-schedule-control { display: flex; align-items: center; flex-wrap: wrap; gap: 9px; margin-top: 8px; }
.group-schedule-menu { display: grid; gap: 7px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: #fbfcfd; }
.group-schedule-menu > button, .group-schedule-menu .schedule-custom button { border: 0; border-radius: 8px; padding: 9px 10px; color: var(--blue); background: var(--light-blue); cursor: pointer; text-align: left; }
.shared-group-composer { grid-template-columns: 42px 42px minmax(0, 1fr) auto; }
.shared-group-composer .schedule-menu, .shared-group-composer .emoji-menu { left: 58px; }

@media (max-width: 680px) {
  .topbar { height: 56px; }
  .brand-mark { width: 30px; height: 30px; }
  .page-shell { padding: 0; }
  .inbox-panel, .thread-panel { border: 0; border-radius: 0; box-shadow: none; }
  .inbox-panel {
    min-height: calc(100vh - 56px);
    min-height: calc(100dvh - 56px);
    min-height: calc(var(--usable-viewport-height, 100dvh) - 56px);
  }
  .thread-panel {
    height: calc(100vh - 56px);
    height: calc(100dvh - 56px);
    height: calc(var(--usable-viewport-height, 100dvh) - 56px);
  }
  .section-header { padding: 18px 15px 10px; }
  .section-header { align-items: flex-start; }
  .section-actions { flex-direction: column-reverse; align-items: stretch; }
  .section-actions button, .section-actions a { width: 100%; text-align: center; }
  .section-header .muted { display: none; }
  .sync-status { margin-left: 15px; margin-right: 15px; }
  .search-row { padding: 8px 15px; }
  .inbox-tabs { padding-left: 15px; }
  .conversation-row { padding: 11px 15px; }
  .group-mode-heading { padding-left: 15px; padding-right: 15px; }
  .conversation-meta time { display: none; }
  .thread-header { padding: 9px 10px; }
  .thread-tools { gap: 7px; }
  .voice-call-button { min-height: 34px; padding: 6px 9px; }
  .message-thread { padding: 14px 10px; }
  .bubble { max-width: 86%; }
  .composer { padding-left: 9px; padding-right: 9px; }
  .form-card, .auth-card { margin: 18px 12px; padding: 22px 18px; }
  .group-detail-heading { align-items: flex-start; flex-direction: column; }
}

html,
body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

.page-shell,
.thread-panel,
.thread-header,
.message-thread,
.message,
.bubble,
.composer {
  min-width: 0;
  max-width: 100%;
}

.thread-panel { width: 100%; }
.message-thread { overflow-x: hidden; }
.message { width: 100%; }
.bubble { min-width: 0; overflow-wrap: anywhere; word-break: break-word; }
.composer { grid-template-columns: 42px 42px 42px minmax(0, 1fr) auto; }
.composer textarea { min-width: 0; }

@media (max-width: 420px) {
  .composer { grid-template-columns: 38px 38px 38px minmax(0, 1fr) auto; gap: 6px; }
  .attach-button, .schedule-button, .emoji-button { width: 38px; height: 38px; }
  .emoji-menu { left: 9px; width: calc(100% - 18px); grid-template-columns: repeat(7, 1fr); }
  .send-button { padding-left: 12px; padding-right: 12px; }
  .shared-group-composer { grid-template-columns: 38px 38px minmax(0, 1fr) auto; gap: 6px; }
}

@media (max-width: 680px) {
  .thread-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    column-gap: 10px;
    row-gap: 5px;
  }

  .thread-back-target {
    grid-column: 1;
    width: 100%;
    padding: 2px 4px;
  }

  .thread-tools {
    grid-column: 2;
    justify-content: flex-start;
    flex-wrap: wrap;
    font-size: 12px;
  }
}
