* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #f5f6fa;
    --sidebar: #2c3e50;
    --primary: #3498db;
    --success: #27ae60;
    --danger: #e74c3c;
    --warn: #f39c12;
    --text: #2c3e50;
    --text-light: #7f8c8d;
    --border: #dcdde1;
    --card: #fff;
    --radius: 6px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    height: 100vh;
    overflow: hidden;
    /* 鎖定 body，捲動交給 .content */
}

.page {
    display: none;
}

.page.active {
    display: flex;
}

/* ── Login ── */
#login-page {
    height: 100vh;
    align-items: center;
    justify-content: center;
    background: var(--sidebar);
}

.login-card {
    background: var(--card);
    padding: 40px;
    border-radius: 8px;
    width: 360px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.login-card h1 {
    text-align: center;
    margin-bottom: 24px;
    font-size: 20px;
}

.login-card input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 12px;
    font-size: 14px;
}

.login-card button {
    width: 100%;
    padding: 10px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    cursor: pointer;
}

.login-card button:hover {
    opacity: 0.9;
}

.error {
    color: var(--danger);
    font-size: 13px;
    margin-top: 8px;
}

/* ── Sidebar ── */
.sidebar {
    width: 200px;
    min-height: 100vh;
    background: var(--sidebar);
    color: #ecf0f1;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    top: 0;
}

.sidebar-header {
    padding: 0 16px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h2 {
    font-size: 16px;
}

.sidebar-header span {
    font-size: 12px;
    color: #95a5a6;
}

.nav-list {
    list-style: none;
    padding: 12px 0;
    flex: 1;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    color: #bdc3c7;
    text-decoration: none;
    font-size: 1rem;
    transition: all 0.15s;
}

.nav-link:hover,
.nav-link.active {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.btn-logout {
    margin: 16px;
    padding: 8px;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #bdc3c7;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
}

.btn-logout:hover {
    border-color: var(--danger);
    color: var(--danger);
}

/* ── Content ── */
.content {
    margin-left: 200px;
    padding: 24px;
    flex: 1;
    height: 100vh;
    overflow-y: auto;
    /* 只有右側內容區可獨立垂直捲動 */
}

.panel {
    display: none;
}

.panel.active {
    display: block;
}

.panel h2 {
    margin-bottom: 16px;
    font-size: 18px;
}

/* ── Stats ── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--card);
    padding: 20px;
    border-radius: var(--radius);
    text-align: center;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.stat-num {
    font-size: 28px;
    font-weight: 700;
    color: var(--primary);
}

.stat-label {
    font-size: 13px;
    color: var(--text-light);
    margin-top: 4px;
}

.stats-detail {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.detail-card {
    background: var(--card);
    padding: 16px;
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.detail-card h3 {
    font-size: 14px;
    margin-bottom: 12px;
    color: var(--text-light);
}

.detail-card table {
    width: 100%;
}

.detail-card td {
    padding: 4px 8px;
    font-size: 13px;
}

.detail-card td:last-child {
    text-align: right;
    font-weight: 600;
}

/* ── 總覽：區段標題 / 範圍選擇 ── */
.section-title {
    font-size: 14px;
    color: var(--text-light);
    margin: 20px 0 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.inline-select {
    padding: 3px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 12px;
    background: var(--card);
}

.stats-grid-8 {
    grid-template-columns: repeat(4, 1fr);
}

.stat-card.stat-warn .stat-num {
    color: #e65100;
}

/* ── 用戶詳情：區塊 / 操作列 / 危險區 ── */
.user-section {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.user-section h4 {
    font-size: 13px;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.user-section textarea {
    width: 100%;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: inherit;
    resize: vertical;
}

.action-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 6px;
}

.action-row input[type="number"],
.action-row input[type="text"] {
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 12px;
}

.result-msg {
    font-size: 12px;
    margin-top: 6px;
    min-height: 1em;
}

.danger-zone {
    border-top-color: var(--danger);
}

.danger-zone h4 {
    color: var(--danger);
}

.btn-small.btn-outline {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
}

/* ── 系統資訊 ── */
.warn-list {
    background: #fff3e0;
    border: 1px solid #ffcc80;
    border-radius: var(--radius);
    padding: 10px 14px 10px 28px;
    margin-bottom: 16px;
    color: #e65100;
    font-size: 13px;
}

.ok-box {
    background: #e8f8f0;
    border: 1px solid #a5d6a7;
    border-radius: var(--radius);
    padding: 10px 14px;
    margin-bottom: 16px;
    color: var(--success);
    font-size: 13px;
}

.mini-table td {
    padding: 3px 6px;
    font-size: 12px;
}

.mini-table th {
    font-size: 12px;
    text-align: left;
    padding: 3px 6px;
    color: var(--text-light);
}

/* ── KMS Widget ── */
.kms-card {
    margin-top: 16px;
}

.kms-provider {
    margin-top: 4px;
    font-size: 13px;
    color: #666;
}

.kms-op-label {
    margin-top: 14px;
    font-size: 12px;
    font-weight: 600;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.kms-stats {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 12px;
    margin-top: 8px;
}

.kms-stats-op {
    grid-template-columns: repeat(4, 1fr);
}

.kms-info-btn {
    margin-left: 6px;
    padding: 0;
    border: none;
    background: none;
    color: #aaa;
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
    vertical-align: middle;
}

.kms-info-btn:hover {
    color: #666;
}

.kms-info-box {
    display: none;
    margin-top: 6px;
    padding: 8px 10px;
    background: #f5f5f5;
    border-left: 3px solid #ccc;
    border-radius: 4px;
    font-size: 12px;
    line-height: 1.5;
    color: #555;
    text-transform: none;
    letter-spacing: normal;
    font-weight: 400;
}

.kms-info-box.open {
    display: block;
}

@media (max-width: 768px) {
    .kms-stats,
    .kms-stats-op {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ── Filters ── */
.filters {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: center;
}

.filters input,
.filters select {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    background: #fff;
}

.filters input[type="text"] {
    width: 250px;
}

.filters input[type="date"] {
    width: 150px;
}

.btn-primary {
    padding: 7px 16px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
}

.btn-primary:hover {
    opacity: 0.9;
}

.btn-small {
    padding: 4px 10px;
    font-size: 12px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
}

.btn-small:hover {
    opacity: 0.9;
}

/* ── Tables ── */
.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

thead th {
    background: #f8f9fa;
    padding: 10px 12px;
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-light);
    text-transform: uppercase;
    border-bottom: 2px solid var(--border);
}

tbody td {
    padding: 8px 12px;
    font-size: .95rem;
    border-bottom: 1px solid #f0f0f0;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

tbody tr:hover {
    background: #f8f9fa;
}

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: .8rem;
    font-weight: 600;
}

.badge-ok {
    background: #e8f8f0;
    color: var(--success);
}

.badge-fail {
    background: #fde8e8;
    color: var(--danger);
}

.badge-google {
    background: #e8f0fe;
    color: #4285f4;
}

.badge-disabled {
    background: #fde8e8;
    color: var(--danger);
}

.badge-comp {
    background: #f3e8fd;
    color: #7b1fa2;
}

.badge-sub-active {
    background: #e8f8f0;
    color: var(--success);
}

.badge-sub-canceled {
    background: #fde8e8;
    color: var(--danger);
}

.badge-sub-past_due {
    background: #fff3e0;
    color: #e65100;
}

.badge-sub-trialing {
    background: #e8f0fe;
    color: #1976d2;
}

.badge-sub-trial {
    background: #e8f8f0;
    color: #2e7d32;
}

.badge-sub-warning {
    background: #fff3e0;
    color: #b45309;
}

.badge-sub-unsubscribed {
    background: #e5e7eb;
    color: #4b5563;
}

.meta-cell {
    font-family: monospace;
    font-size: 11px;
    color: var(--text-light);
    display: inline-block;
    max-width: 250px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: bottom;
}

.log-meta-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 12px 0 6px;
}

.log-meta-pre {
    background: #f6f8fa;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-all;
    max-height: 50vh;
    overflow-y: auto;
}

.uid-cell {
    font-family: monospace;
    font-size: 12px;
    cursor: pointer;
    color: var(--primary);
}

.uid-cell:hover {
    text-decoration: underline;
}

/* ── Pagination ── */
.pagination {
    display: flex;
    gap: 4px;
    margin-top: 12px;
    align-items: center;
    font-size: 13px;
    color: var(--text-light);
}

.pagination button {
    padding: 5px 12px;
    border: 1px solid var(--border);
    background: #fff;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
}

.pagination button:hover {
    border-color: var(--primary);
}

.pagination button:disabled {
    opacity: 0.4;
    cursor: default;
}

/* ── Modal ── */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 100;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--card);
    padding: 24px;
    border-radius: 8px;
    width: 920px;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    font-size: 2.5rem;
    cursor: pointer;
    color: var(--text-light);
}

.modal-close:hover {
    color: var(--text);
}

.modal-content h3 {
    margin-bottom: 16px;
}

.user-info {
    margin-bottom: 16px;
}

.user-info dt {
    font-weight: 600;
    font-size: 12px;
    color: var(--text-light);
    margin-top: 10px;
}

.user-info dd {
    font-size: 14px;
    padding: 2px 0;
}

.quota-adjust {
    border-top: 1px solid var(--border);
    padding-top: 16px;
    margin-top: 16px;
}

.quota-adjust h4 {
    margin-bottom: 8px;
    font-size: 14px;
}

.quota-adjust input {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    width: 160px;
    font-size: 13px;
    margin-right: 8px;
}

#quota-result {
    margin-top: 8px;
    font-size: 13px;
}

/* ── User Devices Panel ── */
.user-devices-panel {
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.user-devices-panel h4 {
    margin: 0 0 12px;
    font-size: 14px;
    color: var(--text);
}

.user-devices-list {
    font-size: 13px;
}

.devices-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12.5px;
}

.devices-table th,
.devices-table td {
    text-align: left;
    padding: 6px 8px;
    border-bottom: 1px solid var(--border);
}

.devices-table th {
    background: var(--bg);
    font-weight: 600;
    color: var(--text-muted);
}

.device-id-short {
    font-family: monospace;
    cursor: pointer;
    border-bottom: 1px dashed var(--text-muted);
}

.device-id-short:hover {
    color: var(--primary);
}

.btn-danger {
    background: var(--danger, #b91c1c);
    color: #fff;
    border: none;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
}

.btn-danger:hover {
    opacity: 0.9;
}

/* ── Responsive ── */
@media (max-width: 768px) {
    .sidebar {
        width: 60px;
        padding: 12px 0;
    }

    .sidebar-header h2,
    .sidebar-header span,
    .nav-link {
        font-size: 0;
    }

    .content {
        margin-left: 60px;
        padding: 16px;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .stats-detail {
        grid-template-columns: 1fr;
    }
}

/* ── 資料檢視（DEV）────────────────────────────────────── */
.dev-badge {
    display: inline-block;
    background: var(--warn);
    color: #fff;
    font-size: .8rem;
    font-weight: 700;
    line-height: 1;
    padding: 4px 7px;
    border-radius: 4px;
    vertical-align: middle;
}

.data-hint {
    color: var(--text-light);
    font-size: 13px;
    margin-bottom: 16px;
}

.data-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: var(--text-light);
}

.data-field select,
.data-field input {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    color: var(--text);
    background: #fff;
}

.data-field-grow {
    flex: 1;
    min-width: 220px;
}

.data-field-grow input {
    width: 100%;
}

.schema-cards {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 18px;
}

.field-card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 12px;
    background: #fff;
    min-width: 110px;
}

.field-card.is-pk {
    border-color: var(--primary);
}

.field-name {
    font-weight: 600;
    font-size: 13px;
}

.field-type {
    font-size: 11px;
    color: var(--text-light);
    margin-top: 2px;
}

.field-badge {
    font-size: 10px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 3px;
    vertical-align: middle;
}

.field-badge.pk {
    background: var(--primary);
    color: #fff;
}

.field-badge.sensitive {
    background: #fde8e8;
    color: var(--danger);
}

#d-table-el td.editable-cell {
    cursor: pointer;
}

#d-table-el td.editable-cell:hover {
    background: #eaf4fd;
}

#d-table-el td.cell-editing {
    padding: 2px;
}

#d-table-el td.cell-editing input {
    width: 100%;
    box-sizing: border-box;
    padding: 5px 7px;
    border: 1px solid var(--primary);
    border-radius: 4px;
    font-size: 13px;
}

#d-table-el td.cell-locked {
    background: #f4f5f7;
    color: var(--text-light);
}

#d-table-el td.cell-saved {
    background: #e8f8f0;
    transition: background .3s;
}

/* 資料檢視表格：橫向捲動 + 垂直捲動時表頭固定 */
#page-data .table-wrap {
    max-height: 60vh;
    overflow: auto;
}

/* 覆蓋全域 table { overflow:hidden }，否則 table 自成捲動容器使 sticky 失效 */
/* width:max-content 讓表格依內容撐開（超過容器才會觸發橫向捲動）；min-width:100% 在欄位少時仍填滿 */
#page-data #d-table-el {
    overflow: visible;
    width: max-content;
    min-width: 100%;
}

#page-data #d-table-el thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    /* border-bottom 在 sticky + border-collapse 下會跟著捲走，改用 box-shadow 補線 */
    box-shadow: inset 0 -2px 0 var(--border);
}