:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1d2430;
  --muted: #6b7280;
  --border: #e2e5ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #15803d;
  --warn: #b45309;
  --error: #b91c1c;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
h2 small { font-weight: normal; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.error { color: var(--error); }
.success { color: var(--ok); }
.notice { background: #fff7e6; border: 1px solid #f5d9a8; padding: .6rem .8rem; border-radius: var(--radius); }

/* Layout */
.topbar { display: flex; align-items: center; gap: 2rem; padding: 0 1.5rem; height: 52px;
          background: #111827; color: #fff; }
.topbar .brand { color: #fff; font-weight: 600; font-size: 1.05rem; }
.topbar nav { display: flex; gap: .25rem; flex: 1; }
.topbar nav a { color: #cbd5e1; padding: .45rem .8rem; border-radius: 6px; }
.topbar nav a.active, .topbar nav a:hover { color: #fff; background: #1f2937; text-decoration: none; }
.topbar .logout { color: #cbd5e1; }
.container { max-width: 1280px; margin: 1.25rem auto; padding: 0 1.25rem; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
        padding: 1rem 1.25rem; margin-bottom: 1.25rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
.grid-2 > .card { margin-bottom: 1.25rem; overflow-x: auto; }
@media (max-width: 1000px) { .grid-2 { grid-template-columns: 1fr; } }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.25rem; margin-bottom: 1.25rem; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; }
.stat .value { display: block; font-size: 1.5rem; font-weight: 600; }
.stat .label { color: var(--muted); }
.stat .value.ok { color: var(--ok); }
.stat .value.warn { color: var(--warn); }
.stat .value.error { color: var(--error); font-size: 1.15rem; }
.stat .up { color: var(--ok); }
.stat .down { color: var(--error); }
a.stat-link { color: inherit; text-decoration: none; }
a.stat-link:hover { border-color: var(--primary); }
.chart-box { position: relative; height: 260px; }
.chart-box.small { height: 180px; }
.chart-box.io-chart { height: 220px; }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem 1rem;
             margin-bottom: .5rem; }
.card-head h2 { margin: 0; }
.chart-filter { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; }
.chart-filter button { font: inherit; font-size: .85rem; padding: .2rem .7rem; border: 1px solid var(--border);
                       border-radius: 999px; background: #fff; color: var(--text); cursor: pointer; }
.chart-filter button:hover { background: #f3f4f6; }
.chart-filter button.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chart-filter select { font-size: .85rem; padding: .2rem .5rem; }
.chart-filter select.active { border-color: var(--primary); color: var(--primary); }
td .badge { margin-left: .35rem; }
td.break { overflow-wrap: anywhere; }
.mailbox-months .io { text-align: right; white-space: nowrap; }
.mailbox-months .io .sep { color: var(--border); }
.mailbox-months .io .out { color: var(--muted); }
.mailbox-months .io.strong { font-weight: 600; }
.mailbox-months tfoot th, .mailbox-months tfoot td { color: var(--text); font-weight: 600; border-bottom: none; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: .85rem; }
tr.current td { background: #eef4ff; }
table.headers th { width: 120px; border: none; padding: .25rem .5rem .25rem 0; }
table.headers td { border: none; padding: .25rem 0; }
.snippet { color: var(--muted); font-size: .85rem; margin-top: .15rem; max-width: 720px;
           overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* max-width: 0 lets the cell shrink to the table width so long snippets get an ellipsis */
td.subject-cell { width: 55%; max-width: 0; }
td.subject-cell .snippet { max-width: none; }

/* Badges and tags */
.badge { display: inline-block; padding: .1rem .45rem; border-radius: 999px; font-size: .78rem;
         background: #eef0f3; color: var(--muted); }
.badge.ok { background: #dcfce7; color: var(--ok); }
.badge.warn { background: #fef3c7; color: var(--warn); }
.badge.inbound { background: #e0ecff; color: #1e40af; }
.badge.outbound { background: #ede9fe; color: #5b21b6; }
.badge.internal { background: #eef0f3; color: #374151; }
.tag { display: inline-block; margin: 0 .3rem .3rem 0; padding: .1rem .45rem; border: 1px solid var(--border);
       border-radius: 4px; font-size: .8rem; color: var(--text); }

/* Forms */
input, select, textarea { font: inherit; padding: .45rem .6rem; border: 1px solid var(--border);
                          border-radius: 6px; background: #fff; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfd3ff; border-color: var(--primary); }
label { display: flex; flex-direction: column; gap: .3rem; color: var(--muted); font-size: .85rem; }
.btn { display: inline-block; font: inherit; padding: .45rem .9rem; border-radius: 6px; cursor: pointer;
       border: 1px solid var(--border); background: #fff; color: var(--text); }
.btn:hover { background: #f3f4f6; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.link { border: none; background: none; color: var(--primary); padding-left: 0; padding-right: 0; }
.btn.danger { color: var(--error); }
.btn.full { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.actions { display: flex; align-items: center; gap: .6rem; margin-top: .75rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem 1rem; }
.form-grid .actions, .form-grid .test-result { grid-column: 1 / -1; }
.test-result p { margin: 0; }

.search-form .search-row { display: flex; gap: .6rem; }
.search-form .search-row input { flex: 1; font-size: 1rem; }
.search-form .filter-row { display: flex; flex-wrap: wrap; gap: .75rem; margin-top: .75rem; align-items: flex-end; }
.pager { display: flex; align-items: center; justify-content: center; gap: 1rem; margin-top: 1rem; }

/* Message */
h1.subject { margin-bottom: .75rem; }
pre.body { white-space: pre-wrap; word-wrap: break-word; font-family: inherit; line-height: 1.5;
           border-top: 1px solid var(--border); padding-top: 1rem; margin: 1rem 0 0; }

/* Login */
main.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card { width: 340px; display: flex; flex-direction: column; gap: 1rem; }
.login-card.wide { width: 460px; }
.login-card h1 { font-size: 1.3rem; margin: 0; }
.login-card p { margin: 0; }
.login-card a.small { text-align: center; }
.login-card a.btn { text-align: center; }
.setup-steps { margin: 0; padding-left: 1.2rem; display: flex; flex-direction: column; gap: .6rem; }
.secret-row { display: flex; align-items: center; gap: .5rem; margin: .35rem 0; }
.secret { font-size: 1rem; letter-spacing: .05em; background: #f3f4f6; padding: .35rem .55rem; border-radius: 6px;
          user-select: all; }
.btn.small { padding: .2rem .6rem; font-size: .85rem; }
.recovery-codes { margin: 0; padding: .75rem 1rem; background: #f3f4f6; border-radius: 6px; font-size: 1rem;
                  line-height: 1.7; columns: 2; user-select: all; }

/* Chat */
main.chat-layout { display: grid; grid-template-columns: 260px 1fr; height: calc(100vh - 52px); }
.chat-sidebar { border-right: 1px solid var(--border); background: #fff; padding: 1rem; overflow-y: auto; }
.session-list { list-style: none; padding: 0; margin: 1rem 0 0; }
.session-list li a { display: block; padding: .45rem .6rem; border-radius: 6px; color: var(--text);
                     overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-list li.active a, .session-list li a:hover { background: #eef2f7; text-decoration: none; }
.chat-main { display: flex; flex-direction: column; min-height: 0; padding: 0 1.5rem 1rem; }
.messages { flex: 1; overflow-y: auto; padding: 1.25rem 0; display: flex; flex-direction: column; gap: 1rem; }
.msg { display: flex; flex-direction: column; max-width: 860px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.bubble { padding: .7rem 1rem; border-radius: 12px; line-height: 1.5; }
.msg.user .bubble { background: var(--primary); color: #fff; white-space: pre-wrap; }
.msg.assistant .bubble { background: #fff; border: 1px solid var(--border); }
.bubble.error-bubble { border-color: #f3c2c2; background: #fff5f5; color: var(--error); }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown table { margin: .5rem 0; }
.markdown pre { background: #f3f4f6; padding: .6rem; border-radius: 6px; overflow-x: auto; }
.markdown code { background: #f3f4f6; padding: 0 .25rem; border-radius: 4px; }
a.ref { font-size: .8em; vertical-align: super; margin: 0 1px; }
.sources { margin: .5rem 0 0 .25rem; font-size: .85rem; }
.sources summary { cursor: pointer; color: var(--muted); }
.sources ol { margin: .25rem 0; padding-left: 1.5rem; }
.chat-form { display: flex; gap: .6rem; align-items: flex-end; }
.chat-form textarea { flex: 1; resize: vertical; min-height: 48px; max-height: 240px; }
.chat-delete { margin-top: .4rem; }
.thinking { color: var(--muted); padding: .4rem 0; }
.empty-chat { margin: auto; max-width: 560px; text-align: center; }
.examples { list-style: none; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.example { width: 100%; text-align: left; font: inherit; padding: .6rem .8rem; border: 1px solid var(--border);
           border-radius: 8px; background: #fff; cursor: pointer; }
.example:hover { border-color: var(--primary); }

/* Settings */
.tabs { display: flex; gap: .25rem; margin-bottom: 1rem; border-bottom: 1px solid var(--border); }
.tabs a { padding: .55rem 1rem; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 500; }
.sync-form fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem 1rem;
                      margin: 0 0 1rem; }
.sync-form legend { padding: 0 .4rem; font-weight: 600; color: var(--text); }
.form-grid.three { grid-template-columns: repeat(3, 1fr); }
.form-grid.four { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .form-grid.three, .form-grid.four { grid-template-columns: 1fr 1fr; } }
.hint { color: var(--muted); font-size: .8rem; }
p.hint { margin: .3rem 0 .6rem; }
.error-box { background: #fff5f5; border-color: #f3c2c2; color: var(--error); margin-bottom: 1rem; }
.error-box ul { margin: .3rem 0 0; padding-left: 1.2rem; }
.mailbox-head { display: flex; align-items: center; gap: .75rem; margin-bottom: .5rem; }
.mailbox-tools { display: flex; align-items: center; gap: .9rem; margin-bottom: .25rem; }
.mailbox-table-wrap { max-height: 420px; overflow-y: auto; border-top: 1px solid var(--border); }
.mailbox-table thead th { position: sticky; top: 0; background: var(--card); z-index: 1; }
.mailbox-table td:first-child { width: 28px; }
.unselected-note { margin-top: .2rem; }
.other-mailboxes ul { margin: .25rem 0 0; }
.advanced { margin-top: .75rem; }
.advanced summary { cursor: pointer; color: var(--muted); margin-bottom: .5rem; }
.label-row { display: flex; flex-wrap: wrap; align-items: center; gap: .9rem; margin-top: .75rem; }
label.inline { flex-direction: row; align-items: center; gap: .4rem; }
.sync-status { margin-top: .75rem; }
.sync-status p { margin: .3rem 0; }
.progress { height: 6px; background: #eef0f3; border-radius: 999px; margin-top: .35rem; width: 320px; overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); }
.model-summary { grid-column: 1 / -1; min-height: 1.2em; margin-top: -.35rem; }
.warn-text { color: var(--warn); margin-top: .2rem; }
.model-picker { grid-column: 1 / -1; border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .75rem;
                display: none; box-shadow: 0 6px 18px rgba(17, 24, 39, .08); }
.model-picker.open { display: block; }
.model-picker-head { display: flex; align-items: center; gap: .6rem; }
.model-picker-head .btn { margin-left: auto; }
.model-list-msg { margin: .4rem 0 0; }
.model-tools { display: flex; flex-wrap: wrap; align-items: center; gap: .9rem; margin: .5rem 0; }
.model-tools .model-filter { flex: 1; min-width: 220px; }
label.check { flex-direction: row; align-items: center; gap: .35rem; color: var(--text); cursor: pointer; }
.model-table-wrap { max-height: 340px; overflow-y: auto; border-top: 1px solid var(--border); }
.model-table thead th { position: sticky; top: 0; background: var(--card); z-index: 1; }
.model-table th[data-sort] { cursor: pointer; user-select: none; }
.model-table th[data-sort]:hover { color: var(--text); }
.model-table th.sorted-asc::after { content: " \25B2"; font-size: .7em; }
.model-table th.sorted-desc::after { content: " \25BC"; font-size: .7em; }
.model-table tbody tr { cursor: pointer; }
.model-table tbody tr:hover td { background: #f6f8fb; }
.model-table tbody tr.current td { background: #eef4ff; }
.model-table code { font-size: .85rem; }

/* htmx */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: block; }
span.htmx-request.htmx-indicator, .actions .htmx-request.htmx-indicator { display: inline; }
