:root {
    --bg: #f4f5f7;
    --card: #ffffff;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --nav: #0f172a;
    --nav-ink: #cbd5e1;
    --accent: #2563eb;
    --warn: #b45309;
    --danger: #dc2626;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
    font: 14px/1.65 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }
h1, h2, h3 { margin: 0 0 10px; font-weight: 650; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; color: var(--muted); font-weight: 600; }
code { background: #eef2f7; padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.app { display: flex; min-height: 100vh; }

/* ---------------------------------------------------------------- sidebar */
.sidebar {
    width: 208px;
    flex: 0 0 208px;
    background: var(--nav);
    color: var(--nav-ink);
    display: flex;
    flex-direction: column;
    padding: 18px 12px 16px;
    position: sticky;
    top: 0;
    height: 100vh;
}

.brand {
    font-size: 18px;
    font-weight: 750;
    color: #fff;
    letter-spacing: .3px;
    padding: 6px 10px 22px;
    display: block;
}
.brand span { color: #60a5fa; }

.nav { display: flex; flex-direction: column; gap: 3px; }
.nav a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px 12px;
    border-radius: 8px;
    color: var(--nav-ink);
    font-size: 14px;
}
.nav a:hover { background: #1e293b; color: #fff; }
.nav a.active { background: #1d4ed8; color: #fff; }
.nav em {
    font-style: normal;
    background: #334155;
    color: #e2e8f0;
    font-size: 11px;
    border-radius: 10px;
    padding: 0 7px;
    line-height: 18px;
}
.nav em.warn { background: #b45309; color: #fff; }

.side-foot { margin-top: auto; }
.ghost {
    width: 100%;
    background: transparent;
    border: 1px solid #334155;
    color: #94a3b8;
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
}
.ghost:hover { color: #fff; border-color: #475569; }

/* ------------------------------------------------------------------- main */
.main { flex: 1; min-width: 0; padding-bottom: 48px; }

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 28px;
    background: rgba(255, 255, 255, .92);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 20;
}
.topbar-title { font-size: 16px; font-weight: 650; }

.content { padding: 20px 28px; max-width: 1280px; }

/* ------------------------------------------------------------------ cards */
.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
    overflow: hidden;
}

/* ------------------------------------------------------------------ flash */
.flash { padding: 0 28px; }
.flash-ok, .flash-err {
    margin-top: 14px;
    padding: 9px 14px;
    border-radius: 8px;
    font-size: 13px;
}
.flash-ok { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.flash-err { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.test-result { background: #eff6ff; border-color: #bfdbfe; }

/* ---------------------------------------------------------------- toolbar */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filter-card { padding: 12px 14px; margin-bottom: 14px; }
.spacer { flex: 1; }

.seg {
    display: inline-flex;
    background: #eef1f5;
    border-radius: 9px;
    padding: 3px;
}
.seg a {
    padding: 6px 12px;
    border-radius: 7px;
    font-size: 13px;
    color: var(--muted);
}
.seg a.active { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, .06); }

/* ---------------------------------------------------------------- controls */
input, select, textarea {
    font: inherit;
    color: var(--ink);
    background: #fff;
    border: 1px solid #d7dbe0;
    border-radius: 8px;
    padding: 7px 10px;
    outline: none;
    max-width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
input[type="search"] { min-width: 210px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 7px 14px;
    border-radius: 8px;
    border: 1px solid #d7dbe0;
    background: #fff;
    color: var(--ink);
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.subtle { color: var(--muted); }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.block { width: 100%; }
.mini {
    display: inline-block;
    padding: 3px 9px;
    border-radius: 6px;
    border: 1px solid #d7dbe0;
    background: #fff;
    font-size: 12px;
    cursor: pointer;
    color: #374151;
}
.mini:hover { background: #f3f4f6; }
.mini.danger { color: var(--danger); border-color: #fecaca; }
.hint { color: var(--muted); font-size: 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.smallb { font-size: 14px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* ------------------------------------------------------------------ table */
.table { width: 100%; border-collapse: collapse; }
.table th {
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    padding: 8px 10px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}
.table td {
    padding: 10px;
    border-bottom: 1px solid #f0f2f5;
    vertical-align: top;
    font-size: 13px;
}
.table tr:last-child td { border-bottom: 0; }
.table.compact td, .table.compact th { padding: 7px 10px; }
.table tr:hover td { background: #fafbfc; }
.table tr.unread .subject { font-weight: 650; }
.right { text-align: right; }
.clip { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.w-from { width: 190px; }
.w-date { width: 150px; }

.from-name { font-weight: 600; }
.from-addr { font-size: 12px; color: var(--muted); word-break: break-all; }
.subject { display: block; color: var(--ink); }
.subject:hover { color: var(--accent); }
.snippet {
    font-size: 12px;
    color: var(--muted);
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 640px;
}
.date { color: var(--muted); font-size: 12px; white-space: nowrap; }
.tags { white-space: nowrap; }

.tag {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 20px;
    background: #f1f3f6;
    color: #4b5563;
    font-size: 11px;
    line-height: 19px;
    margin-right: 4px;
}
.tag.blue { background: #e0ecff; color: #1d4ed8; }
.tag.warn { background: #fef3c7; color: #92400e; }

.row-actions { display: flex; gap: 5px; flex-wrap: wrap; align-items: center; }
.row-actions form { margin: 0; }

/* ------------------------------------------------------------------ stats */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}
.stat {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.stat span { font-size: 12px; color: var(--muted); }
.stat b { font-size: 22px; font-weight: 700; letter-spacing: -.02em; }

/* ------------------------------------------------------------------ pager */
.pager {
    display: flex;
    align-items: center;
    gap: 12px;
    justify-content: center;
    color: var(--muted);
    font-size: 13px;
    padding: 6px 0 10px;
}

/* ------------------------------------------------------------------- mail */
.back { display: inline-block; margin-bottom: 12px; color: var(--muted); font-size: 13px; }
.back:hover { color: var(--accent); }

.mail-head h1 { margin-bottom: 8px; word-break: break-word; }
.mail-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 18px;
    font-size: 12px;
    color: var(--muted);
}

.mail-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.body-card { padding: 0; }
.mail-body { width: 100%; height: 66vh; border: 0; display: block; background: #fff; }
.side-col { display: flex; flex-direction: column; gap: 16px; }

.att { border-top: 1px solid #f1f3f6; padding: 10px 0; }
.att:first-of-type { border-top: 0; }
.att-name { font-size: 13px; font-weight: 600; word-break: break-all; }
.att-meta { display: flex; gap: 10px; font-size: 11px; color: var(--muted); margin: 2px 0 6px; }
.att-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.att-status { margin-top: 6px; }

.inv-item {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 0;
    border-top: 1px solid #f1f3f6;
    font-size: 13px;
}
.inv-item:first-of-type { border-top: 0; }
.inv-item:hover { color: var(--accent); }

/* ----------------------------------------------------------------- detail */
.detail-grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(340px, .75fr); gap: 16px; align-items: start; }
.preview-card { padding: 0; }
.preview-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--line);
    font-size: 12px;
    color: var(--muted);
}
.pdf-frame { width: 100%; height: 74vh; border: 0; display: block; background: #f8fafc; }

.form-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.form-card label input, .form-card label select { display: block; width: 100%; margin-top: 4px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field-row.three { grid-template-columns: 1fr 1fr 1fr; }
.actions { display: flex; gap: 10px; margin-top: 6px; flex-wrap: wrap; }
.actions.separated { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.actions form { margin: 0; }

/* ------------------------------------------------------------------- misc */
.empty { text-align: center; padding: 40px 20px; color: var(--muted); font-size: 13px; }
.empty.small { padding: 18px 10px; }
.empty p { margin: 4px 0; }

.tips ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
.tips li { margin: 4px 0; }

.mode-form { display: flex; flex-direction: column; gap: 10px; max-width: 560px; margin-top: 12px; }
.radio {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px;
    cursor: pointer;
}
.radio:has(input:checked) { border-color: var(--accent); background: #f5f8ff; }
.radio.disabled { opacity: .55; cursor: not-allowed; }
.radio span { display: flex; flex-direction: column; gap: 2px; }
.radio small { color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------------ login */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: radial-gradient(1200px 600px at 50% -10%, #1e3a8a 0%, #0f172a 55%, #0b1120 100%);
}
.login-card {
    width: 360px;
    background: #fff;
    border-radius: 16px;
    padding: 32px 28px;
    box-shadow: 0 24px 60px rgba(0, 0, 0, .35);
}
.login-brand { font-size: 24px; font-weight: 750; letter-spacing: .2px; }
.login-brand span { color: var(--accent); }
.login-sub { color: var(--muted); font-size: 13px; margin: 4px 0 20px; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.login-card label input { display: block; width: 100%; margin-top: 5px; padding: 9px 11px; }
.login-card .btn { margin-top: 6px; padding: 10px; }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 1000px) {
    .mail-layout, .detail-grid { grid-template-columns: 1fr; }
    .pdf-frame { height: 52vh; }
}
@media (max-width: 760px) {
    .app { flex-direction: column; }
    .sidebar {
        width: 100%;
        flex: none;
        height: auto;
        position: static;
        flex-direction: row;
        align-items: center;
        gap: 10px;
        padding: 10px 12px;
        overflow-x: auto;
    }
    .brand { padding: 0 8px 0 2px; font-size: 16px; }
    .nav { flex-direction: row; }
    .nav a { padding: 6px 10px; }
    .nav em { display: none; }
    .side-foot { margin: 0; }
    .ghost { width: auto; padding: 6px 10px; }
    .content, .flash, .topbar { padding-left: 16px; padding-right: 16px; }
    .w-from, .w-date { width: auto; }
    .snippet { max-width: 60vw; }
}
