:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --text: #172033;
  --muted: #657086;
  --line: #e5e9f1;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --warning: #d97706;
  --success: #0f9f6e;
  --shadow: 0 10px 30px rgba(31, 45, 70, 0.07);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--text); background: var(--bg); line-height: 1.55; }
a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }
button, input, select, textarea { font: inherit; }
.container { width: min(1180px, calc(100% - 32px)); margin: 0 auto; }
.page { min-height: calc(100vh - 132px); padding: 32px 0 56px; }
.topbar { background: #13233f; color: #fff; box-shadow: 0 3px 12px rgba(13, 27, 51, .2); }
.topbar-inner { height: 68px; display: flex; align-items: center; justify-content: space-between; }
.brand { display: inline-flex; align-items: center; gap: 11px; color: #fff; font-weight: 700; font-size: 18px; }
.brand:hover { color: #fff; }
.brand-mark { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 9px; background: #3b82f6; color: #fff; }
.brand-mark.large { width: 48px; height: 48px; border-radius: 13px; font-size: 22px; }
.nav { display: flex; align-items: center; gap: 20px; }
.nav a, .nav .link-button { color: #d8e2f2; }
.nav-user { color: #99a9c2; padding-left: 10px; border-left: 1px solid #324565; }
.nav form, .heading-actions form, .attachment-actions form { margin: 0; }
.link-button { border: 0; background: none; padding: 0; color: var(--primary); cursor: pointer; }
.footer { border-top: 1px solid var(--line); color: var(--muted); background: #fff; padding: 20px 0; font-size: 13px; text-align: center; }

.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.page-heading h1 { margin: 4px 0; font-size: 28px; letter-spacing: -.02em; }
.page-heading p, .section-heading p { margin: 4px 0 0; color: var(--muted); }
.title-line { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.back-link { display: inline-block; margin-bottom: 6px; font-size: 14px; }
.heading-actions { display: flex; gap: 10px; align-items: center; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 20px; }
.stat-card { padding: 20px; background: #fff; border: 1px solid var(--line); border-left: 4px solid var(--primary); border-radius: 12px; }
.stat-card.warning { border-left-color: var(--warning); }
.stat-card.danger { border-left-color: var(--danger); }
.stat-card span { color: var(--muted); font-size: 14px; }
.stat-card strong { display: block; margin-top: 4px; font-size: 28px; }

.btn { display: inline-flex; justify-content: center; align-items: center; min-height: 40px; padding: 8px 16px; border: 1px solid transparent; border-radius: 9px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); color: #fff; }
.btn-secondary { background: #eef3fb; color: #254264; border-color: #dce4ef; }
.btn-ghost { background: #fff; border-color: var(--line); color: #45546c; }
.btn-danger-outline { color: var(--danger); border-color: #fecaca; background: #fff; }
.btn-block { width: 100%; }
.btn:disabled { cursor: not-allowed; opacity: .55; }
.form-control, .form-select { width: 100%; min-height: 42px; padding: 9px 11px; border: 1px solid #d5dbe6; border-radius: 8px; background: #fff; color: var(--text); outline: none; }
.form-check-input { width: 18px; height: 18px; margin: 0; accent-color: var(--primary); }
textarea.form-control { min-height: 95px; resize: vertical; }
.form-control:focus, .form-select:focus { border-color: #7aa7ff; box-shadow: 0 0 0 3px rgba(37, 99, 235, .11); }

.filter-bar { display: grid; grid-template-columns: minmax(250px, 1fr) 180px 160px auto auto; gap: 10px; padding: 18px; border-bottom: 1px solid var(--line); }
.filter-bar.audit-filter { grid-template-columns: minmax(280px, 1fr) 190px auto auto; }
.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { padding: 12px 14px; background: #f8fafc; color: #58657b; text-align: left; font-weight: 600; border-bottom: 1px solid var(--line); }
td { padding: 14px; border-bottom: 1px solid #edf0f5; vertical-align: middle; }
tbody tr:hover { background: #fbfcff; }
tbody tr:last-child td { border-bottom: 0; }
td small { display: block; color: var(--muted); margin-top: 3px; max-width: 250px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.table-title { color: var(--text); font-weight: 650; }
.mono { font-family: "SFMono-Regular", Consolas, monospace; letter-spacing: .02em; }
.table-actions { display: flex; gap: 12px; }
.actions-col { width: 110px; }
.pagination { display: flex; justify-content: center; align-items: center; gap: 18px; padding: 17px; border-top: 1px solid var(--line); color: var(--muted); font-size: 14px; }

.badge { display: inline-flex; align-items: center; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 650; background: #eef2f7; color: #58657b; }
.badge-valid, .badge-active, .badge-long_term { background: #e8f8f2; color: #087c56; }
.badge-expiring { background: #fff4dd; color: #a95b00; }
.badge-expired, .badge-revoked { background: #feecec; color: #bd1f1f; }
.badge-cancelled { background: #eef1f5; color: #566273; }

.form-card { padding: 24px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px 22px; }
.form-group label, .upload-fields label span, .stack-form label span { display: block; margin-bottom: 6px; font-size: 14px; font-weight: 600; }
.span-2 { grid-column: span 2; }
.required { margin-left: 4px; color: var(--danger); font-size: 11px; }
.field-error { display: block; color: var(--danger); margin-top: 5px; }
.help-text { display: block; color: var(--muted); margin-top: 5px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 26px; padding-top: 20px; border-top: 1px solid var(--line); }

.settings-layout { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, .85fr); gap: 20px; align-items: start; }
.settings-form { padding: 0; overflow: hidden; }
.settings-section { padding: 24px; border-bottom: 1px solid var(--line); }
.settings-section-heading { align-items: flex-start; }
.settings-section-heading h2 { font-size: 18px; }
.toggle-row { display: flex; align-items: flex-start; gap: 11px; padding: 15px; border: 1px solid #d9e4f4; border-radius: 10px; background: #f7faff; cursor: pointer; }
.toggle-row input { margin-top: 3px; flex: 0 0 auto; }
.toggle-row strong, .toggle-row small { display: block; }
.toggle-row small { margin-top: 3px; color: var(--muted); }
.settings-advanced { margin: 0 24px; border-bottom: 1px solid var(--line); }
.settings-advanced summary { padding: 19px 0; color: #334a68; cursor: pointer; font-weight: 650; }
.settings-advanced-body { padding: 2px 0 24px; }
.checkbox-field { display: flex; flex-direction: column; justify-content: center; }
.checkbox-field label { display: flex; align-items: center; gap: 9px; margin: 0; cursor: pointer; }
.checkbox-field label span { font-size: 14px; font-weight: 600; }
.settings-note { display: flex; gap: 9px; margin: 22px 24px 0; padding: 13px 15px; border-radius: 10px; background: #eff6ff; color: #315477; font-size: 14px; }
.settings-note strong { flex: 0 0 auto; color: #1e4f91; }
.settings-form > .form-actions { margin: 20px 24px 0; padding-bottom: 24px; }
.settings-sidebar { display: grid; gap: 16px; }
.settings-status-card { padding: 20px; }
.settings-status-list { margin: 0; }
.settings-status-list > div { padding: 11px 0; border-top: 1px solid var(--line); }
.settings-status-list dt { margin-bottom: 3px; color: var(--muted); font-size: 12px; }
.settings-status-list dd { margin: 0; overflow-wrap: anywhere; font-size: 14px; }
.settings-test-form { margin-top: 16px; }
.settings-warning { padding: 17px 18px; border: 1px solid #f4d7a4; border-radius: 12px; background: #fffbeb; color: #76521c; font-size: 13px; }
.settings-warning strong { display: block; margin-bottom: 7px; color: #75460b; }
.settings-warning p { margin: 7px 0 0; }

.detail-card, .attachment-section { padding: 24px; margin-bottom: 20px; }
.section-heading { display: flex; justify-content: space-between; margin-bottom: 19px; }
.section-heading h2 { margin: 0; font-size: 20px; }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); margin: 0; border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.detail-grid > div { padding: 14px 16px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.detail-grid dt { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.detail-grid dd { margin: 0; }
.pre-line { white-space: pre-line; }
.attachment-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.attachment-card { overflow: hidden; border: 1px solid var(--line); border-radius: 11px; background: #fff; }
.attachment-preview { height: 170px; display: grid; place-items: center; background: #f1f4f8; border-bottom: 1px solid var(--line); }
.attachment-preview img { width: 100%; height: 100%; object-fit: contain; }
.file-icon { display: grid; place-items: center; width: 64px; height: 76px; color: #fff; background: #dc2626; border-radius: 8px; font-weight: 800; }
.attachment-body { padding: 14px; }
.attachment-type { display: inline-block; margin-bottom: 7px; color: var(--primary); font-size: 12px; font-weight: 700; }
.attachment-body strong, .attachment-body small { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attachment-body small { margin-top: 5px; color: var(--muted); }
.attachment-body p { margin: 8px 0 0; color: var(--muted); font-size: 13px; }
.attachment-actions { display: flex; align-items: center; gap: 12px; margin-top: 12px; padding-top: 11px; border-top: 1px solid var(--line); font-size: 14px; }
.danger-text { color: var(--danger); }
.upload-panel { display: grid; grid-template-columns: 220px 1fr; gap: 24px; margin-top: 22px; padding: 18px; border-radius: 11px; background: #f7f9fc; border: 1px dashed #cbd5e1; }
.upload-panel h3 { margin: 0; font-size: 17px; }
.upload-panel p { margin: 5px 0 0; color: var(--muted); font-size: 13px; }
.upload-fields { display: grid; grid-template-columns: 170px minmax(220px, 1fr) minmax(180px, 1fr) auto; gap: 10px; align-items: end; }

.message-stack { display: grid; gap: 9px; margin-bottom: 18px; }
.message { padding: 11px 14px; border-radius: 9px; border: 1px solid #dbe6f8; background: #eff6ff; color: #244c84; }
.message-success { background: #ecfdf5; color: #0a6b4c; border-color: #bcebd8; }
.message-error { background: #fff1f2; color: #a51d31; border-color: #fecdd3; }
.message-warning { background: #fffbeb; color: #945b0b; border-color: #fde68a; }
.empty-state { padding: 48px 20px !important; text-align: center; color: var(--muted); }
.empty-state strong, .empty-state span { display: block; }
.empty-state strong { color: var(--text); margin-bottom: 5px; }
.attachment-empty { grid-column: 1 / -1; border: 1px dashed #ccd5e2; border-radius: 10px; }
.login-shell { min-height: calc(100vh - 210px); display: grid; place-items: center; }
.login-card { width: min(430px, 100%); padding: 30px; background: #fff; border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); }
.login-heading { display: flex; align-items: center; gap: 14px; margin-bottom: 24px; }
.login-heading h1 { margin: 0; font-size: 24px; }
.login-heading p { margin: 3px 0 0; color: var(--muted); }
.stack-form { display: grid; gap: 16px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.error-page { min-height: 55vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.error-page > strong { color: #c9d2df; font-size: 72px; line-height: 1; }
.error-page h1 { margin: 15px 0 5px; }
.error-page p { margin: 0 0 20px; color: var(--muted); }

@media (max-width: 900px) {
  .filter-bar { grid-template-columns: 1fr 1fr; }
  .search-field { grid-column: 1 / -1; }
  .attachment-grid { grid-template-columns: repeat(2, 1fr); }
  .upload-panel { grid-template-columns: 1fr; }
  .upload-fields { grid-template-columns: 1fr 1fr; }
  .settings-layout { grid-template-columns: 1fr; }
}

@media (max-width: 650px) {
  .container { width: min(100% - 22px, 1180px); }
  .page { padding-top: 22px; }
  .brand > span:last-child, .nav-user { display: none; }
  .nav { max-width: calc(100% - 48px); gap: 10px; overflow-x: auto; font-size: 13px; scrollbar-width: none; }
  .nav::-webkit-scrollbar { display: none; }
  .page-heading { align-items: flex-start; flex-direction: column; }
  .heading-actions { width: 100%; }
  .stats-grid, .form-grid, .detail-grid, .attachment-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
  .filter-bar, .upload-fields { grid-template-columns: 1fr; }
  .search-field { grid-column: auto; }
  .attachment-preview { height: 220px; }
  .form-card, .detail-card, .attachment-section { padding: 17px; }
  .settings-form { padding: 0; }
  .settings-section { padding: 17px; }
  .settings-advanced { margin: 0 17px; }
  .settings-note { flex-direction: column; margin: 18px 17px 0; }
  .settings-form > .form-actions { margin: 18px 17px 0; padding-bottom: 17px; }
}
