:root {
    --bg: #f6f7f9;
    --panel: #ffffff;
    --ink: #1f2430;
    --muted: #6b7280;
    --line: #e5e7eb;
    --accent: #2f6df6;
    --accent-soft: #eaf0fe;
    --radius: 10px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    font-weight: 700;
    letter-spacing: 0.04em;
    color: var(--accent);
}

.search { flex: 1; }
.search input {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    font-size: 14px;
    background: #fafafa;
}
.search input:focus { outline: none; border-color: var(--accent); background: #fff; }

.actions { display: flex; gap: 8px; align-items: center; }

select, .btn {
    font-size: 13px;
    padding: 8px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    cursor: pointer;
}
.btn:hover { background: #f3f4f6; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #2559d6; }
.btn:disabled { opacity: 0.6; cursor: default; }

.banner {
    padding: 10px 16px;
    background: var(--accent-soft);
    color: #1b4bbf;
    font-size: 13px;
    border-bottom: 1px solid var(--line);
}

.layout {
    display: grid;
    grid-template-columns: 220px 1fr;
    min-height: calc(100vh - 53px);
}

.sidebar {
    border-right: 1px solid var(--line);
    background: var(--panel);
    padding: 12px;
}
.categories { display: flex; flex-direction: column; gap: 2px; }
.categories button {
    text-align: left;
    padding: 8px 10px;
    border: none;
    background: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    color: var(--ink);
    display: flex;
    justify-content: space-between;
}
.categories button:hover { background: #f3f4f6; }
.categories button.active { background: var(--accent-soft); color: #1b4bbf; font-weight: 600; }
.categories .count { color: var(--muted); font-variant-numeric: tabular-nums; }

.list { padding: 0; }
.status { padding: 12px 16px; color: var(--muted); font-size: 13px; }

.messages { list-style: none; margin: 0; padding: 0; }
.msg {
    display: grid;
    grid-template-columns: 220px 1fr auto;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
    align-items: baseline;
}
.msg.unread .subject { font-weight: 700; }
.msg:hover { background: #fbfcff; }
.msg .from { color: var(--ink); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .from .email { color: var(--muted); font-size: 12px; display: block; }
.msg .subject { font-size: 14px; }
.msg .snippet { color: var(--muted); font-weight: 400; }
.msg .meta { text-align: right; white-space: nowrap; }
.msg .date { color: var(--muted); font-size: 12px; }

.tag {
    display: inline-block;
    font-size: 11px;
    padding: 2px 7px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: #1b4bbf;
    margin-left: 6px;
}
.tag.prio-high { background: #fde8e8; color: #b42318; }
.tag.prio-low { background: #eef2f5; color: var(--muted); }

/* --- Settings modal --- */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(31, 36, 48, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
    background: var(--panel);
    border-radius: 14px;
    padding: 22px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
.modal-card h2 { margin: 0 0 16px; font-size: 17px; }
.modal-card .check { display: flex; align-items: center; gap: 8px; font-size: 14px; margin-bottom: 16px; }
.modal-card .field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.modal-card .field select { width: 100%; }
.modal-card .hint { font-size: 12px; color: var(--muted); min-height: 16px; margin: 0 0 14px; }
.modal-card .hint.warn { color: #b42318; }
.modal-actions { display: flex; align-items: center; gap: 8px; }
.modal-actions .spacer { flex: 1; }

/* --- Email reader --- */
.reader-card { max-width: 860px; width: 100%; height: 84vh; display: flex; flex-direction: column; padding: 0; }
.reader-head { display: flex; align-items: flex-start; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.reader-headings { flex: 1; min-width: 0; }
.reader-subject { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.reader-meta { font-size: 12px; color: var(--muted); word-break: break-word; }
.reader-summary { font-size: 12px; color: #1b4bbf; margin-top: 6px; }
.reader-summary:empty { display: none; }
.reader-body { flex: 1; width: 100%; border: 0; background: #fff; border-radius: 0 0 14px 14px; }
.msg { cursor: pointer; }

/* --- Rules editor --- */
.rules-card { max-width: 720px; width: 100%; max-height: 88vh; overflow-y: auto; padding: 0; }
.rules-toolbar { display: flex; align-items: center; gap: 10px; padding: 12px 18px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.btn.small { font-size: 12px; padding: 5px 9px; }
.rules-list { list-style: none; margin: 0; padding: 8px 0; }
.rule-row { display: flex; align-items: center; gap: 10px; padding: 10px 18px; border-bottom: 1px solid var(--line); }
.rule-row:hover { background: #fbfcff; }
.rule-row .rule-info { flex: 1; min-width: 0; }
.rule-row .rule-name { font-weight: 600; font-size: 14px; }
.rule-row .rule-desc { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rule-row.disabled { opacity: 0.55; }
.rule-editor { padding: 18px; }
.editor-section { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 18px 0 8px; }
.cond-row, .act-row { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }
.cond-row select, .cond-row input, .act-row select, .act-row input { padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; min-width: 0; }
.cond-row .f-field { flex: 0 0 130px; }
.cond-row .f-op { flex: 0 0 120px; }
.cond-row .f-val { flex: 1; }
.act-row .a-type { flex: 0 0 150px; }
.act-row .a-val { flex: 1; }
.row-x { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 0 6px; }
.row-x:hover { color: #b42318; }

/* --- Login screen --- */
.login {
    position: fixed; inset: 0; z-index: 200;
    background: linear-gradient(160deg, #e9eefb, #f6f7f9);
    display: flex; align-items: center; justify-content: center; padding: 20px;
}
.login[hidden] { display: none; }
.login-card {
    background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
    padding: 28px 26px; width: 100%; max-width: 360px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.12);
    display: flex; flex-direction: column; gap: 12px;
}
.login-brand { font-weight: 800; letter-spacing: 0.08em; color: var(--accent); font-size: 22px; text-align: center; }
.login-sub { text-align: center; color: var(--muted); font-size: 12px; margin: -8px 0 6px; }
.login-msg { font-size: 13px; color: var(--muted); text-align: center; margin: 0; }
.login-msg:empty { display: none; }
.login-card input {
    width: 100%; padding: 11px 12px; border: 1px solid var(--line);
    border-radius: var(--radius); font-size: 16px; background: #fafafa; /* 16px avoids iOS zoom */
}
.login-card input:focus { outline: none; border-color: var(--accent); background: #fff; }
.login-submit { padding: 11px; font-size: 15px; }
.login-error { color: #b42318; font-size: 13px; min-height: 16px; text-align: center; }
.login-error:empty { display: none; }

/* --- Mobile / narrow vertical --- */
@media (max-width: 720px) {
    .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 10px; }
    .brand { order: 1; }
    .actions { order: 2; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; }
    .search { order: 3; flex-basis: 100%; }
    select, .btn { font-size: 12px; padding: 7px 9px; }
    #account-filter { max-width: 42vw; }

    .layout { grid-template-columns: 1fr; min-height: 0; }
    .sidebar { display: none; }

    .msg { grid-template-columns: 1fr; gap: 2px; padding: 12px 14px; }
    .msg .from { white-space: normal; }
    .msg .subject { display: inline; }
    .msg .meta { text-align: left; }

    /* Full-screen sheets on phones */
    .modal { padding: 0; align-items: stretch; }
    .modal-card { max-width: none; width: 100%; height: 100%; max-height: 100%; border-radius: 0; overflow-y: auto; }
    .reader-card, .rules-card { height: 100%; max-height: 100%; border-radius: 0; }
    .reader-body { border-radius: 0; }

    /* Rule editor rows stack instead of overflowing */
    .cond-row, .act-row { flex-wrap: wrap; }
    .cond-row .f-field, .cond-row .f-op, .cond-row .f-val,
    .act-row .a-type, .act-row .a-val { flex: 1 1 100%; }
    .row-x { flex: 0 0 auto; }
}
