:root {
    --bg: #f4f7fb;
    --panel: #ffffff;
    --text: #172033;
    --muted: #667085;
    --line: #e4e9f2;
    --primary: #3157d5;
    --primary-dark: #2445b4;
    --success: #14804a;
    --warning: #a15c00;
    --danger: #c9363e;
    --shadow: 0 8px 28px rgba(31, 50, 91, 0.07);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }

body {
    margin: 0;
    min-height: 100vh;
    color: var(--text);
    background: var(--bg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    font-size: 14px;
    line-height: 1.55;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    min-height: 64px;
    padding: 0 32px;
    background: rgba(255, 255, 255, 0.96);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(10px);
}

.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand:hover { color: var(--text); }
.brand-mark { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 10px; color: #fff; background: linear-gradient(145deg, #4169e1, #253da5); font-weight: 800; box-shadow: 0 5px 14px rgba(49, 87, 213, 0.25); }

.nav { display: flex; align-self: stretch; gap: 4px; margin-left: 42px; }
.nav a { display: flex; align-items: center; padding: 0 15px; color: #4b5568; border-bottom: 2px solid transparent; }
.nav a:hover, .nav a.active { color: var(--primary); background: #f7f9ff; border-bottom-color: var(--primary); }

.account { display: flex; align-items: center; gap: 14px; margin-left: auto; }
.account form { margin: 0; }
.link-button { padding: 0; color: var(--muted); background: none; border: 0; cursor: pointer; font: inherit; }
.link-button:hover { color: var(--danger); }

.container { width: min(1380px, calc(100% - 48px)); margin: 0 auto; padding: 28px 0 56px; }
.page-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.page-heading h1 { margin: 0; font-size: 25px; letter-spacing: -0.02em; }

.alert { margin-bottom: 18px; padding: 12px 16px; border: 1px solid; border-radius: 10px; }
.alert-success { color: #0b6840; background: #ecf9f2; border-color: #b9e8cf; }
.alert-error { color: #9e2630; background: #fff0f1; border-color: #f5c3c7; }
.alert-warning { color: #855000; background: #fff8e8; border-color: #f0d69e; }

.panel { margin-bottom: 20px; padding: 24px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.panel h2 { margin: 0; font-size: 18px; }
.panel p { margin: 4px 0 0; color: var(--muted); }
.panel-header { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.narrow-panel { max-width: 640px; }

.stat-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; margin-bottom: 20px; }
.compact-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.stat-card { padding: 21px 22px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.stat-card-accent { color: #fff; background: linear-gradient(135deg, #3157d5, #203a9a); border-color: transparent; }
.stat-card-accent .stat-meta, .stat-card-accent .stat-meta a { color: #dfe6ff; }
.stat-top { display: flex; align-items: center; justify-content: space-between; color: var(--muted); }
.stat-card-accent .stat-top { color: #dfe6ff; }
.stat-card strong { display: block; margin-top: 10px; font-size: 34px; line-height: 1.1; }
.stat-meta { margin-top: 10px; color: var(--muted); font-size: 13px; }
.denomination-tag { padding: 3px 9px; border-radius: 20px; font-weight: 700; }
.denomination-50 { color: #205bb5; background: #eaf3ff; }
.denomination-100 { color: #7a3db5; background: #f3eaff; }

.button { display: inline-flex; align-items: center; justify-content: center; min-height: 38px; padding: 8px 15px; border: 1px solid transparent; border-radius: 9px; cursor: pointer; font: inherit; font-weight: 600; transition: 0.15s ease; }
.button:hover { transform: translateY(-1px); }
.button:disabled { opacity: 0.62; cursor: wait; transform: none; }
.button-primary { color: #fff; background: var(--primary); }
.button-primary:hover { color: #fff; background: var(--primary-dark); }
.button-secondary { color: #354054; background: #fff; border-color: #ccd4e2; }
.button-secondary:hover { color: var(--primary); border-color: #9cacdc; }
.button-warning { color: #fff; background: #b96c06; }
.button-warning:hover { color: #fff; background: #9d5900; }
.button-danger { color: #fff; background: var(--danger); }
.button-danger:hover { color: #fff; background: #ad2831; }
.button-large { min-height: 46px; padding: 11px 20px; }
.button-block { width: 100%; }
.button-group { display: flex; align-items: center; gap: 10px; }
.button-group form { margin: 0; }

label { display: grid; gap: 7px; color: #354054; font-weight: 600; }
input, select, textarea { width: 100%; color: var(--text); background: #fff; border: 1px solid #cbd3e1; border-radius: 9px; outline: none; font: inherit; transition: border-color 0.15s, box-shadow 0.15s; }
input, select { height: 42px; padding: 8px 11px; }
textarea { padding: 10px 11px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: #6b86df; box-shadow: 0 0 0 3px rgba(49, 87, 213, 0.12); }
input[type="checkbox"], input[type="radio"] { width: 17px; height: 17px; padding: 0; accent-color: var(--primary); }
label small { color: var(--muted); font-weight: 400; }
.required { color: var(--danger); }
.form-grid { display: grid; gap: 18px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.two-column { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 0.8fr); gap: 20px; align-items: start; }
.help-panel ol { margin: 14px 0 0; padding-left: 22px; color: #4c566b; }
.help-panel li + li { margin-top: 11px; }

.denomination-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 0; padding: 0; border: 0; }
.denomination-picker legend { grid-column: 1 / -1; margin-bottom: 2px; color: #354054; font-weight: 600; }
.denomination-option { position: relative; display: block; }
.denomination-option input { position: absolute; opacity: 0; pointer-events: none; }
.denomination-option span { display: flex; align-items: center; justify-content: space-between; padding: 16px; border: 1px solid #cbd3e1; border-radius: 11px; cursor: pointer; }
.denomination-option small { color: var(--muted); }
.denomination-option input:checked + span { color: var(--primary); background: #f4f7ff; border-color: var(--primary); box-shadow: 0 0 0 2px rgba(49, 87, 213, 0.1); }

.filter-panel { padding: 18px 24px; }
.filter-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; align-items: end; }

.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; white-space: nowrap; }
th, td { padding: 12px 13px; text-align: left; border-bottom: 1px solid #e9edf4; }
th { color: #667085; background: #f8fafc; font-size: 12px; font-weight: 700; letter-spacing: 0.02em; }
tbody tr:hover { background: #fafbfe; }
tbody tr:last-child td { border-bottom: 0; }
.checkbox-cell { width: 44px; text-align: center; }
.mono, .mono-link { font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; }
.mono-link { font-size: 13px; }
.card-code-cell { color: #27344d; }
.log-details { max-width: 380px; overflow: hidden; text-overflow: ellipsis; }
.modal-backdrop[hidden] { display: none; }
.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; background: rgba(15, 23, 42, 0.58); backdrop-filter: blur(3px); }
.sync-modal { width: min(560px, 100%); padding: 25px; background: #fff; border-radius: 16px; box-shadow: 0 24px 80px rgba(15, 23, 42, 0.28); }
.sync-modal-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.sync-modal-header h2 { margin: 3px 0 0; font-size: 20px; }
.modal-close { width: 34px; height: 34px; padding: 0; color: var(--muted); background: #f4f6f9; border: 0; border-radius: 50%; cursor: pointer; font-size: 22px; }
.modal-close:disabled { opacity: 0.4; cursor: not-allowed; }
.sync-modal > p { margin: 15px 0; color: var(--muted); }
.progress-track { height: 12px; overflow: hidden; background: #e8edf5; border-radius: 20px; }
.progress-bar { width: 0; height: 100%; background: linear-gradient(90deg, #3157d5, #5a7be4); border-radius: inherit; transition: width 0.25s ease; }
.progress-numbers { display: flex; gap: 20px; margin-top: 12px; color: var(--muted); }
.success-text { color: var(--success); }
.error-text { color: var(--danger); }
.sync-current-status { min-height: 48px; margin-top: 16px; padding: 12px 14px; color: #4c566b; background: #f8fafc; border-radius: 9px; }
.sync-result-list { max-height: 210px; margin-top: 12px; overflow-y: auto; border: 1px solid var(--line); border-radius: 9px; }
.sync-result-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
.sync-result-row:last-child { border-bottom: 0; }
.sync-result-row.ok strong { color: var(--success); }
.sync-result-row.failed strong { color: var(--danger); }
.sync-modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.badge-issued { color: #1e55aa; background: #eaf3ff; }
.badge-available { color: #087647; background: #e8f8ef; }
.badge-returned { color: #5d6574; background: #edf0f4; }
.badge-partially_returned { color: #965500; background: #fff3d9; }
.badge-unknown { color: #5d6574; background: #edf0f4; }
.badge-unactivated { color: #5d6574; background: #edf0f4; }
.badge-active { color: #087647; background: #e8f8ef; }
.badge-exhausted { color: #965500; background: #fff3d9; }
.badge-disabled { color: #a5232d; background: #ffebed; }
.badge-expired { color: #6b3ca1; background: #f2eaff; }
.badge-bound { color: #8a4c00; background: #fff0d7; }
.sync-error { display: inline-block; margin-left: 5px; color: var(--danger); font-size: 12px; cursor: help; }

.usage-summary-grid { display: flex; flex-wrap: wrap; gap: 12px; }
.usage-summary-item { display: flex; align-items: center; gap: 7px; min-width: 150px; padding: 12px 14px; background: #f8fafc; border-radius: 9px; }
.usage-summary-item strong { margin-left: auto; font-size: 18px; }
.sync-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 15px; padding: 12px 14px; color: var(--muted); background: #f8fafc; border-radius: 9px; }

.eyebrow { color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; }
.batch-number { margin-top: 3px !important; font-family: "SFMono-Regular", Consolas, monospace; }
.detail-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; margin: 0; }
.detail-grid div { padding: 13px 15px; background: #f8fafc; border-radius: 9px; }
.detail-grid dt { color: var(--muted); font-size: 12px; }
.detail-grid dd { margin: 4px 0 0; font-weight: 600; }
.note-box { margin-top: 18px; padding: 13px 15px; color: #4b5568; background: #fffaf0; border: 1px solid #f2dfba; border-radius: 9px; }
.code-output { min-height: 160px; color: #15213a; background: #f8fafc; font-family: "SFMono-Regular", Consolas, monospace; line-height: 1.8; }
.return-actions { display: grid; grid-template-columns: minmax(280px, 1fr) auto; gap: 14px; align-items: end; margin-top: 18px; }
.danger-zone { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-top: 24px; padding: 18px; background: #fff8f8; border: 1px solid #f2c9cc; border-radius: 10px; }
.danger-zone p { margin: 2px 0 0; }
.inline-return-form { display: flex; align-items: end; gap: 10px; }
.inline-return-form input { min-width: 260px; }

.pagination { display: flex; align-items: center; justify-content: center; gap: 18px; margin-top: 22px; color: var(--muted); }
.pagination a { padding: 6px 12px; background: #fff; border: 1px solid #d6dce7; border-radius: 8px; }
.empty-state { padding: 46px 20px; color: var(--muted); text-align: center; }

.login-page { display: grid; place-items: center; background: radial-gradient(circle at 20% 10%, #eef2ff 0, transparent 36%), radial-gradient(circle at 80% 90%, #e9f5ff 0, transparent 32%), #f5f7fb; }
.login-shell { width: min(420px, calc(100% - 32px)); padding: 40px 0; }
.login-card { padding: 34px; background: rgba(255, 255, 255, 0.97); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 20px 60px rgba(37, 55, 99, 0.13); }
.login-brand { display: flex; justify-content: center; margin-bottom: 15px; }
.login-brand .brand-mark { width: 44px; height: 44px; font-size: 20px; }
.login-card h1 { margin: 0; text-align: center; font-size: 23px; }
.login-card > p { margin: 7px 0 25px; color: var(--muted); text-align: center; }
.login-card form { display: grid; gap: 17px; }

@media (max-width: 980px) {
    .topbar { flex-wrap: wrap; padding: 10px 18px 0; }
    .brand { margin-bottom: 10px; }
    .account { margin-bottom: 10px; }
    .nav { order: 3; width: calc(100% + 36px); height: 48px; margin: 0 -18px; overflow-x: auto; border-top: 1px solid var(--line); }
    .nav a { flex: 1 0 auto; justify-content: center; }
    .container { width: min(100% - 28px, 1380px); padding-top: 22px; }
    .stat-grid { grid-template-columns: 1fr 1fr; }
    .stat-card-accent { grid-column: 1 / -1; }
    .two-column { grid-template-columns: 1fr; }
    .filter-form { grid-template-columns: 1fr 1fr; }
    .detail-grid { grid-template-columns: 1fr 1fr; }
    .danger-zone, .inline-return-form { align-items: stretch; flex-direction: column; }
    .sync-toolbar { align-items: stretch; flex-direction: column; }
    .inline-return-form input { min-width: 0; }
}

@media (max-width: 620px) {
    .container { width: min(100% - 20px, 1380px); }
    .page-heading h1 { font-size: 22px; }
    .panel { padding: 18px; border-radius: 11px; }
    .stat-grid, .compact-stats { grid-template-columns: 1fr; gap: 12px; }
    .stat-card-accent { grid-column: auto; }
    .field-row, .denomination-picker, .filter-form, .detail-grid, .return-actions { grid-template-columns: 1fr; }
    .panel-header { align-items: flex-start; flex-direction: column; }
    .button-group { width: 100%; flex-wrap: wrap; }
    .button-group .button, .button-group form { flex: 1; }
    .button-group form .button { width: 100%; }
    .account > a { display: none; }
    .login-card { padding: 26px 22px; }
}
