:root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #fafbfc;
    --border: #dfe3e8;
    --text: #1c2733;
    --text-muted: #6b7785;
    --accent: #2563eb;
    --accent-text: #ffffff;
    --danger: #dc2626;
    --ok: #15803d;
    --warn: #b45309;
    --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
    --radius: 8px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14181d;
        --surface: #1c2128;
        --surface-2: #232931;
        --border: #313a45;
        --text: #e6eaef;
        --text-muted: #97a3b0;
        --accent: #3b82f6;
        --accent-text: #ffffff;
        --danger: #f05252;
        --ok: #4ade80;
        --warn: #fbbf24;
        --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    }
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
    font-size: 14px;
    line-height: 1.55;
}

.hidden {
    display: none !important;
}

.muted {
    color: var(--text-muted);
}

.small {
    font-size: 12px;
}

.error-text {
    color: var(--danger);
    min-height: 1.2em;
    margin: 6px 0 0;
    font-size: 13px;
}

/* --- 登入 --- */

.login-view {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 28px;
}

.login-card {
    width: 100%;
    max-width: 360px;
}

.login-card h1 {
    margin: 0 0 4px;
    font-size: 20px;
}

.login-card p {
    margin: 0 0 20px;
}

.login-origin {
    margin: 10px 0 0;
    text-align: center;
    word-break: break-all;
}

/* --- 版面 --- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.brand {
    font-weight: 600;
    font-size: 16px;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

main {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

/* --- 統計卡 --- */

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 20px;
}

.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
}

.stat-value {
    font-size: 24px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.stat-label {
    color: var(--text-muted);
    font-size: 12px;
    margin-top: 2px;
}

.stat.attention .stat-value {
    color: var(--warn);
}

.stat.bad .stat-value {
    color: var(--danger);
}

/* --- 分頁籤 --- */

.tabs {
    display: flex;
    gap: 4px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 16px;
}

.tab {
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    padding: 8px 14px;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
}

.tab.active {
    color: var(--text);
    border-bottom-color: var(--accent);
    font-weight: 600;
}

/* --- 工具列 --- */

.toolbar {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    align-items: center;
}

.toolbar input[type="search"] {
    min-width: 220px;
    flex: 1 1 220px;
    max-width: 360px;
}

.toolbar .primary {
    margin-left: auto;
}

/* --- 表單元件 --- */

input, select, button {
    font-family: inherit;
    font-size: 14px;
}

input:not([type="checkbox"]), select {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    width: 100%;
}

input:focus, select:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
}

label {
    display: block;
    margin-bottom: 14px;
}

label > span {
    display: block;
    margin-bottom: 4px;
    font-size: 13px;
    color: var(--text-muted);
}

button {
    padding: 7px 14px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    white-space: nowrap;
}

button:hover:not(:disabled) {
    background: var(--surface-2);
}

button:disabled {
    opacity: .5;
    cursor: not-allowed;
}

button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
}

button.primary:hover:not(:disabled) {
    filter: brightness(1.08);
}

button.danger {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
}

button.ghost {
    background: none;
}

.login-card button[type="submit"] {
    width: 100%;
    margin-top: 4px;
}

/* --- 表格 --- */

.table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    white-space: nowrap;
}

th, td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

th {
    background: var(--surface-2);
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    position: sticky;
    top: 0;
}

tbody tr:last-child td {
    border-bottom: none;
}

tbody tr:hover {
    background: var(--surface-2);
}

td.wrap {
    white-space: normal;
    max-width: 240px;
}

.empty-row td {
    text-align: center;
    color: var(--text-muted);
    padding: 32px;
}

.mono {
    font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
    font-size: 12px;
}

/* --- 狀態徽章 --- */

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
    border: 1px solid transparent;
}

.badge.active {
    color: var(--ok);
    border-color: currentColor;
}

.badge.expired {
    color: var(--danger);
    border-color: currentColor;
}

.badge.suspended {
    color: var(--danger);
    border-color: currentColor;
}

.badge.not_active {
    color: var(--text-muted);
    border-color: currentColor;
}

.badge.warn {
    color: var(--warn);
    border-color: currentColor;
}

/* --- 列操作 --- */

.actions-col {
    width: 1%;
}

.row-actions {
    display: flex;
    gap: 4px;
}

.row-actions button {
    padding: 3px 8px;
    font-size: 12px;
}

/* --- 分頁 --- */

.pager {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 12px;
    color: var(--text-muted);
    font-size: 13px;
}

/* --- 對話框 --- */

dialog {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    padding: 24px;
    width: min(440px, calc(100vw - 32px));
    box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}

dialog::backdrop {
    background: rgba(0, 0, 0, .45);
}

dialog h2 {
    margin: 0 0 16px;
    font-size: 17px;
}

#confirm-message, #password-target {
    white-space: pre-line;
}

dialog menu {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin: 20px 0 0;
    padding: 0;
}

.quick-days {
    display: flex;
    gap: 6px;
    margin: -8px 0 14px;
    flex-wrap: wrap;
}

.quick-days button {
    padding: 4px 10px;
    font-size: 12px;
}

/* --- 提示 --- */

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    padding: 10px 18px;
    border-radius: 999px;
    box-shadow: var(--shadow);
    z-index: 100;
}

.toast.error {
    background: var(--danger);
    color: #fff;
}

@media (max-width: 640px) {
    main {
        padding: 12px;
    }

    .toolbar .primary {
        margin-left: 0;
    }
}
