/* ============================================================
   Maileroo Webmail - giao diện
   Palette trung tính (zinc) + accent indigo. Sáng/tối tự động.
   ============================================================ */

:root {
    --bg:        #f6f7f9;
    --surface:   #ffffff;
    --surface-2: #f1f2f5;
    --border:    #e4e6eb;
    --border-2:  #eceef1;
    --text:      #1f2430;
    --text-dim:  #6b7280;
    --text-faint:#9aa1ad;
    --accent:    #4f46e5;
    --accent-weak:#eef0fe;
    --danger:    #dc2626;
    --ok:        #059669;
    --warn:      #d97706;
    --radius:    12px;
    --radius-sm: 8px;
    --shadow:    0 1px 2px rgba(20,24,40,.04), 0 8px 24px rgba(20,24,40,.06);
    --sidebar-w: 280px;
    --list-w:    384px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg:        #0e1016;
        --surface:   #16181f;
        --surface-2: #1c1f28;
        --border:    #262a35;
        --border-2:  #20242e;
        --text:      #e6e8ee;
        --text-dim:  #9aa1ad;
        --text-faint:#6b7280;
        --accent:    #818cf8;
        --accent-weak:#1e2140;
        --shadow:    0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

/* ---------- Layout 3 khung ---------- */
body.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) var(--list-w) 1fr;
    height: 100dvh;
    overflow: hidden;
}

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 18px 14px;
    font-weight: 650;
    font-size: 15px;
    letter-spacing: -.01em;
}
.brand-mark {
    display: grid; place-items: center;
    width: 32px; height: 32px;
    border-radius: 9px;
    background: var(--accent);
    color: #fff;
    flex: none;
}
.brand-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.side-nav { padding: 4px 10px; display: flex; flex-direction: column; gap: 2px; }

.side-link {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-dim);
    font-weight: 500;
    transition: background .12s, color .12s;
}
.side-link:hover { background: var(--surface-2); color: var(--text); }
.side-link.active { background: var(--accent-weak); color: var(--accent); font-weight: 600; }
.side-ico { display: grid; place-items: center; width: 18px; flex: none; }
.side-link span:nth-child(2) { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
    background: var(--accent);
    color: #fff;
    font-size: 11px;
    font-weight: 650;
    min-width: 19px;
    height: 19px;
    padding: 0 6px;
    border-radius: 999px;
    display: inline-grid;
    place-items: center;
    line-height: 1;
}
.badge.small { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.side-link.active .badge.small { background: var(--accent); color: #fff; border-color: transparent; }

.side-section {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 14px 10px 10px;
    border-top: 1px solid var(--border-2);
    margin-top: 8px;
}
.side-heading {
    font-size: 11px;
    font-weight: 650;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--text-faint);
    padding: 0 10px 8px;
}
.side-empty { color: var(--text-faint); font-size: 12.5px; line-height: 1.55; padding: 4px 10px; }

.domain-tree, .mailbox-list { list-style: none; margin: 0; padding: 0; }

.domain-item details > summary {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text);
    font-weight: 550;
    list-style: none;
    user-select: none;
}
.domain-item details > summary::-webkit-details-marker { display: none; }
.domain-item details > summary:hover { background: var(--surface-2); }
.domain-caret { display: grid; place-items: center; transition: transform .15s; color: var(--text-faint); flex: none; }
.domain-item details[open] > summary .domain-caret { transform: rotate(90deg); }
.domain-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; }

.mailbox-list { padding: 2px 0 6px 6px; margin-left: 10px; border-left: 1px solid var(--border-2); }
.mailbox-link {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    color: var(--text-dim);
    font-size: 13px;
}
.mailbox-link:hover { background: var(--surface-2); color: var(--text); }
.mailbox-link.active { background: var(--accent-weak); color: var(--accent); font-weight: 600; }
.mailbox-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.mailbox-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailbox-all { flex: 1; font-style: italic; color: var(--text-faint); }
.mailbox-link.active .mailbox-all { color: var(--accent); font-style: normal; }

.side-foot { padding: 8px 10px; border-top: 1px solid var(--border-2); }
.side-link.ghost { color: var(--text-dim); }
.side-user { display: flex; align-items: center; gap: 10px; padding: 8px 12px 10px; }
.avatar.sm { width: 30px; height: 30px; font-size: 11px; }
.side-user-info { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.side-user-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-user-role { font-size: 11px; color: var(--text-faint); }

/* ============================================================
   DANH SÁCH EMAIL
   ============================================================ */
.list-pane {
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.list-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-2);
}

.search {
    position: relative;
    flex: 1;
    display: flex;
    align-items: center;
}
.search-ico { position: absolute; left: 12px; color: var(--text-faint); pointer-events: none; }
.search input[type=search] {
    width: 100%;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    border-radius: 999px;
    padding: 9px 34px 9px 34px;
    font-size: 13.5px;
    outline: none;
    transition: border-color .12s, box-shadow .12s;
}
.search input[type=search]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
.search-clear { position: absolute; right: 12px; color: var(--text-faint); font-size: 18px; line-height: 1; }
.search-clear:hover { color: var(--text); }

.list-subhead {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    padding: 12px 16px 8px;
}
.list-title {
    margin: 0;
    font-size: 15px;
    font-weight: 650;
    letter-spacing: -.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.list-count { color: var(--text-faint); font-size: 12px; flex: none; }

.mail-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0 8px 12px; }

.mail-row {
    display: flex;
    gap: 11px;
    padding: 11px 12px;
    border-radius: var(--radius);
    transition: background .1s;
    position: relative;
}
.mail-row:hover { background: var(--surface-2); }
.mail-row.active { background: var(--accent-weak); }
.mail-row.kbd-focus { box-shadow: inset 0 0 0 2px var(--accent); }
.mail-row + .mail-row { margin-top: 1px; }

.avatar {
    width: 38px; height: 38px;
    border-radius: 50%;
    color: #fff;
    display: grid; place-items: center;
    font-size: 13px; font-weight: 650;
    flex: none;
    letter-spacing: .02em;
}
.avatar.lg { width: 44px; height: 44px; font-size: 15px; }

.mail-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.mail-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.mail-sender { font-weight: 550; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-time { color: var(--text-faint); font-size: 11.5px; flex: none; }
.mail-subject {
    font-size: 13px;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: flex; align-items: center; gap: 6px;
}
.mail-preview { font-size: 12.5px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-to { font-size: 11px; color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }

.mail-row.unread .mail-sender,
.mail-row.unread .mail-subject { font-weight: 700; color: var(--text); }
.unread-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.star-flag { color: var(--warn); font-size: 12px; flex: none; }

.pager { display: flex; justify-content: space-between; padding: 14px 6px 4px; }

/* ============================================================
   KHUNG ĐỌC
   ============================================================ */
.read-pane {
    background: var(--bg);
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.read-empty, .empty-state, .read-empty p {
    color: var(--text-faint);
}
.read-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
}
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 60px 20px;
    text-align: center;
}

.read-head {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-2);
    background: var(--surface);
}
.read-head-actions { display: flex; align-items: center; gap: 4px; }
.inline { display: inline; margin: 0; }

.read-body { flex: 1; min-height: 0; overflow-y: auto; padding: 26px clamp(18px, 4vw, 44px) 60px; }
.read-subject {
    margin: 0 0 20px;
    font-size: clamp(19px, 2.4vw, 24px);
    font-weight: 680;
    letter-spacing: -.02em;
    line-height: 1.25;
}

.read-meta { display: flex; gap: 13px; align-items: center; margin-bottom: 14px; }
.read-meta-text { min-width: 0; }
.read-from { font-size: 14.5px; }
.read-from strong { font-weight: 650; }
.read-email { color: var(--text-dim); font-weight: 400; }
.read-to { font-size: 12.5px; color: var(--text-dim); margin-top: 3px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.chip {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 2px 8px;
    font-size: 12px;
    color: var(--text);
}
.read-date { color: var(--text-faint); }

.read-flags { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; }
.flag {
    font-size: 11px;
    font-weight: 600;
    padding: 3px 9px;
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--text-dim);
    border: 1px solid var(--border);
}
.flag.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 30%, transparent); background: color-mix(in srgb, var(--ok) 10%, transparent); }
.flag.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 30%, transparent); background: color-mix(in srgb, var(--warn) 10%, transparent); }
.flag.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); background: color-mix(in srgb, var(--danger) 10%, transparent); }

.fmt-switch { margin-left: auto; display: inline-flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.fmt-switch a { padding: 3px 12px; font-size: 11.5px; font-weight: 600; color: var(--text-dim); }
.fmt-switch a.on { background: var(--accent); color: #fff; }

.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.attach {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: 12.5px;
    transition: border-color .12s, background .12s;
}
.attach:hover { border-color: var(--accent); background: var(--accent-weak); }
.attach.disabled { opacity: .55; cursor: not-allowed; }
.attach-name { font-weight: 550; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-size { color: var(--text-faint); }

.read-content {
    background: var(--surface);
    border: 1px solid var(--border-2);
    border-radius: var(--radius);
    padding: 22px;
    box-shadow: var(--shadow);
}
.html-frame { width: 100%; border: 0; min-height: 200px; background: transparent; display: block; }
pre.plain {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.65;
    color: var(--text);
}

/* ============================================================
   NÚT & TIỆN ÍCH
   ============================================================ */
.icon-btn {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-dim);
    width: 36px; height: 36px;
    border-radius: var(--radius-sm);
    display: grid; place-items: center;
    cursor: pointer;
    transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.icon-btn:active { transform: translateY(1px); }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 9px 16px;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    font-weight: 600;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    transition: background .12s, border-color .12s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-ghost { background: transparent; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); background: var(--accent); }
.btn-block { width: 100%; }

.alert { padding: 10px 14px; border-radius: var(--radius-sm); font-size: 13px; margin-bottom: 14px; }
.alert-error { background: color-mix(in srgb, var(--danger) 10%, transparent); color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 25%, transparent); }

.only-mobile { display: none; }

/* ============================================================
   TRANG ĐĂNG NHẬP
   ============================================================ */
.login-body {
    display: grid;
    place-items: center;
    min-height: 100dvh;
    background:
        radial-gradient(1200px 600px at 50% -10%, var(--accent-weak), transparent 60%),
        var(--bg);
    padding: 20px;
}
.login-card {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 34px 30px;
    box-shadow: var(--shadow);
    text-align: center;
}
.login-mark {
    width: 52px; height: 52px;
    border-radius: 14px;
    background: var(--accent);
    color: #fff;
    display: grid; place-items: center;
    margin: 0 auto 16px;
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; font-weight: 680; letter-spacing: -.02em; }
.login-sub { margin: 0 0 22px; color: var(--text-dim); font-size: 13.5px; }
.login-card label { display: block; text-align: left; font-size: 12.5px; font-weight: 600; color: var(--text-dim); margin-bottom: 7px; }
.login-card input[type=password] {
    width: 100%;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 11px 13px;
    font-size: 14px;
    outline: none;
    margin-bottom: 16px;
    transition: border-color .12s, box-shadow .12s;
}
.login-card input[type=password]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 1040px) {
    body.app { grid-template-columns: var(--sidebar-w) 1fr; }
    body.app.has-view .list-pane { display: none; }
    body.app:not(.has-view) .read-pane { display: none; }
}

@media (max-width: 720px) {
    body.app { grid-template-columns: 1fr; }
    .only-mobile { display: grid; }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: 86%;
        max-width: 320px;
        z-index: 40;
        transform: translateX(-100%);
        transition: transform .22s ease;
        box-shadow: var(--shadow);
    }
    .sidebar.open { transform: translateX(0); }

    body.app[data-mobile-pane="list"] .read-pane { display: none; }
    body.app[data-mobile-pane="list"] .list-pane { display: flex; }
    body.app[data-mobile-pane="read"] .list-pane { display: none; }
    body.app[data-mobile-pane="read"] .read-pane { display: flex; }

    .read-body { padding: 20px 16px 50px; }
}

/* ============================================================
   TRANG QUẢN TRỊ NGƯỜI DÙNG
   ============================================================ */
.admin-body { background: var(--bg); min-height: 100dvh; overflow-y: auto; }
.admin-wrap { max-width: 1100px; margin: 0 auto; padding: 24px 20px 60px; }
.admin-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.admin-head h1 { margin: 0; font-size: 19px; font-weight: 680; letter-spacing: -.02em; }

.alert-ok { background: color-mix(in srgb, var(--ok) 10%, transparent); color: var(--ok); border: 1px solid color-mix(in srgb, var(--ok) 25%, transparent); }

.admin-grid { display: grid; grid-template-columns: 380px 1fr; gap: 18px; align-items: start; }
@media (max-width: 860px) { .admin-grid { grid-template-columns: 1fr; } }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.panel h2 { margin: 0 0 16px; font-size: 15px; font-weight: 650; }
.note { background: var(--surface-2); border: 1px solid var(--border-2); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; color: var(--text-dim); margin-bottom: 14px; }
.note code, .dom-cell code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.form .field { margin-bottom: 14px; display: flex; flex-direction: column; gap: 6px; }
.form label { font-size: 12.5px; font-weight: 600; color: var(--text-dim); }
.form input[type=text], .form input[type=password], .form select {
    border: 1px solid var(--border); background: var(--bg); color: var(--text);
    border-radius: var(--radius-sm); padding: 9px 12px; font-size: 13.5px; outline: none; width: 100%;
    transition: border-color .12s, box-shadow .12s;
}
.form input:focus, .form select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
.hint { font-size: 11.5px; color: var(--text-faint); }
.form-actions { display: flex; gap: 8px; margin-top: 4px; }

.chk { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 400; color: var(--text); cursor: pointer; padding: 4px 0; }
.chk.strong { font-weight: 600; margin-bottom: 6px; }
.chk input { width: auto; }
.chk-list { display: flex; flex-direction: column; gap: 2px; max-height: 180px; overflow-y: auto; border: 1px solid var(--border-2); border-radius: var(--radius-sm); padding: 8px 10px; margin-bottom: 8px; }

.utable { width: 100%; border-collapse: collapse; font-size: 13px; }
.utable th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); padding: 6px 8px; border-bottom: 1px solid var(--border); }
.utable td { padding: 10px 8px; border-bottom: 1px solid var(--border-2); vertical-align: top; }
.dom-cell { display: flex; flex-wrap: wrap; gap: 4px; }
.row-actions { white-space: nowrap; display: flex; gap: 6px; align-items: center; }

.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.tag.admin { background: var(--accent-weak); color: var(--accent); border-color: transparent; }
.tag.all { background: color-mix(in srgb, var(--ok) 12%, transparent); color: var(--ok); border-color: transparent; }
.tag.dom { background: var(--bg); color: var(--text); }
.tag.warn { background: color-mix(in srgb, var(--warn) 12%, transparent); color: var(--warn); border-color: transparent; }

.btn.sm { padding: 5px 11px; font-size: 12px; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* ============================================================
   SOẠN THƯ / GỬI
   ============================================================ */
.compose-btn {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    margin: 4px 14px 10px; padding: 11px 16px;
    background: var(--accent); color: #fff; font-weight: 650; font-size: 13.5px;
    border-radius: 999px; box-shadow: var(--shadow); transition: filter .12s, transform .05s;
}
.compose-btn:hover { filter: brightness(1.06); }
.compose-btn:active { transform: translateY(1px); }

.admin-wrap.narrow { max-width: 760px; }
.form textarea {
    border: 1px solid var(--border); background: var(--bg); color: var(--text);
    border-radius: var(--radius-sm); padding: 12px 14px; font-size: 14px;
    font-family: inherit; line-height: 1.6; outline: none; width: 100%; resize: vertical;
    transition: border-color .12s, box-shadow .12s;
}
.form textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }

.from-row { display: flex; align-items: center; gap: 8px; }
.from-row input { flex: 1; }
.from-row .at { color: var(--text-faint); font-weight: 600; }
.from-row select { flex: 1.3; }

.form-actions.center { justify-content: center; }
.reply-btn { gap: 6px; }

.result-panel { text-align: center; padding: 40px 28px; }
.result-ico { width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center; margin: 0 auto 16px; font-size: 28px; font-weight: 700; }
.result-ico.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
.result-ico.fail { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.result-panel h2 { margin: 0 0 8px; font-size: 18px; }
.err-list { text-align: left; max-width: 420px; margin: 0 auto 18px; color: var(--danger); font-size: 13.5px; }
