:root {
  --bg: #f4f6f7;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --ink: #172126;
  --muted: #66757c;
  --line: #d8e1e4;
  --line-strong: #b9c8ce;
  --blue: #0874d1;
  --blue-dark: #075da5;
  --green: #167c57;
  --green-bg: #eaf7f0;
  --amber: #8a5a05;
  --amber-bg: #fff6df;
  --red: #b43c45;
  --red-bg: #fff0f1;
  --shadow: 0 14px 40px rgba(23, 33, 38, .12);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; letter-spacing: 0; }
button, input, textarea { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: #eef2f3; }
.login-panel { width: min(420px, 100%); padding: 34px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.brand-mark, .brand-symbol { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 6px; background: var(--ink); color: #fff; font-size: 13px; font-weight: 800; }
.login-panel .brand-mark { margin-bottom: 26px; }
.eyebrow { margin: 0; color: var(--blue); font: 700 11px/1 ui-monospace, SFMono-Regular, Consolas, monospace; letter-spacing: 1px; }
h1, h2, p { margin-top: 0; }
.login-panel h1 { margin: 8px 0 28px; font-size: 25px; }
label { display: block; color: var(--muted); font-size: 12px; font-weight: 700; }
label > span { display: block; margin-bottom: 6px; }
input, textarea { width: 100%; border: 1px solid var(--line-strong); border-radius: 5px; background: #fbfcfc; color: var(--ink); outline: none; }
input { height: 40px; padding: 0 11px; }
textarea { min-height: 220px; padding: 12px; resize: vertical; font: 12px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; }
input:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(8, 116, 209, .12); }
.login-panel form { display: grid; gap: 18px; }
.form-error { margin: 14px 0 0; color: var(--red); font-size: 13px; }

.button, .icon-button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 38px; border: 1px solid var(--line-strong); border-radius: 5px; background: var(--surface); color: var(--ink); font-weight: 700; text-decoration: none; cursor: pointer; }
.button { padding: 0 13px; }
.button svg, .icon-button svg, .section-heading > svg, .sync-state svg, .search svg { width: 16px; height: 16px; flex: 0 0 16px; }
.button:hover, .icon-button:hover { border-color: #91a7b0; background: var(--surface-2); }
.button:disabled, .icon-button:disabled { opacity: .5; cursor: wait; }
.button.primary { border-color: var(--blue); background: var(--blue); color: #fff; }
.button.primary:hover { background: var(--blue-dark); }
.button.secondary { background: #f7fafb; }
.button.ghost { border-color: transparent; background: transparent; }
.button.wide { width: 100%; }
.button.compact { min-height: 32px; padding: 0 9px; font-size: 12px; }
.button.danger { border-color: #e4b8bc; color: var(--red); background: var(--red-bg); }
.icon-button { width: 38px; height: 38px; padding: 0; }

.topbar { height: 64px; padding: 0 max(20px, calc((100vw - 1440px) / 2)); display: flex; align-items: center; justify-content: space-between; gap: 20px; border-bottom: 1px solid var(--line); background: var(--surface); }
.brand { display: flex; align-items: center; gap: 11px; color: var(--ink); text-decoration: none; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: 15px; }
.brand small { color: var(--muted); font-size: 11px; }
.brand-symbol { width: 34px; height: 34px; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.sync-state { display: inline-flex; align-items: center; gap: 6px; margin-right: 8px; color: var(--green); font-size: 12px; }

.admin-main { width: min(1440px, calc(100% - 36px)); margin: 26px auto 50px; }
.page-heading { display: flex; align-items: center; justify-content: space-between; min-height: 52px; margin-bottom: 18px; }
.page-heading h1 { margin: 6px 0 0; font-size: 24px; }
.metrics { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 8px; background: var(--surface); overflow: hidden; }
.metrics article { min-width: 0; padding: 18px 20px; border-right: 1px solid var(--line); }
.metrics article:last-child { border-right: 0; }
.metrics span, .metrics small { display: block; color: var(--muted); }
.metrics span { font-size: 12px; font-weight: 700; }
.metrics strong { display: block; margin: 5px 0 1px; font-size: 24px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.metrics small { font-size: 11px; }

.workspace { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 16px; margin-top: 16px; align-items: start; }
.tool-panel { border: 1px solid var(--line); border-radius: 8px; background: var(--surface); overflow: hidden; }
.section-heading, .directory-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 17px 18px; border-bottom: 1px solid var(--line); }
.section-heading h2, .directory-head h2, .dialog-head h2 { margin: 5px 0 0; font-size: 17px; }
.section-heading > svg { color: var(--muted); width: 20px; height: 20px; }
.generator form { display: grid; gap: 15px; padding: 18px; }
.directory-head { align-items: flex-end; }
.directory-tools { display: flex; align-items: center; gap: 10px; }
.search { position: relative; width: 250px; }
.search svg { position: absolute; left: 10px; bottom: 11px; color: var(--muted); }
.search input { padding-left: 34px; }
.segments { display: inline-flex; height: 40px; padding: 3px; border: 1px solid var(--line); border-radius: 5px; background: #f2f5f6; }
.segments button { min-width: 58px; border: 0; border-radius: 3px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 700; cursor: pointer; }
.segments button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(23, 33, 38, .13); }

.table-wrap { overflow: auto; }
table { width: 100%; min-width: 1040px; border-collapse: collapse; }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { background: var(--surface-2); color: var(--muted); font-size: 11px; white-space: nowrap; }
td { font-size: 12px; }
tbody tr:hover { background: #fafcfc; }
.code-cell { display: flex; align-items: center; gap: 5px; }
.code-cell code { color: #184e75; font: 700 11px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace; white-space: nowrap; }
.mini-copy { width: 28px; height: 28px; border: 0; border-radius: 4px; background: transparent; color: var(--muted); cursor: pointer; }
.mini-copy:hover { background: #edf3f5; color: var(--ink); }
.mini-copy svg { width: 14px; height: 14px; }
.number { font-variant-numeric: tabular-nums; }
.note-cell { max-width: 190px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { display: inline-flex; align-items: center; min-height: 24px; padding: 0 8px; border-radius: 4px; font-size: 11px; font-weight: 800; white-space: nowrap; }
.status.active { background: var(--green-bg); color: var(--green); }
.status.used_up { background: var(--amber-bg); color: var(--amber); }
.status.disabled { background: var(--red-bg); color: var(--red); }
.row-actions { display: flex; gap: 5px; justify-content: flex-end; white-space: nowrap; }
.row-actions .icon-button { width: 32px; height: 32px; }
.row-actions .icon-button svg { width: 15px; height: 15px; }
.actions-col { text-align: right; }
.empty { padding: 52px 20px; text-align: center; color: var(--muted); }
.table-footer { min-height: 52px; display: flex; align-items: center; justify-content: space-between; padding: 9px 14px; color: var(--muted); font-size: 12px; }

dialog { width: min(580px, calc(100% - 30px)); max-height: calc(100vh - 40px); padding: 0; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(23, 33, 38, .48); }
.detail-dialog { width: min(900px, calc(100% - 30px)); }
.dialog-head { min-height: 68px; display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dialog-body { padding: 18px; overflow: auto; }
.dialog-body form, .dialog-body label { min-width: 0; }
.dialog-body label + label { margin-top: 15px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 18px; border-top: 1px solid var(--line); }
.dialog-body .dialog-actions { margin: 18px -18px -18px; }
.selected-code { margin-bottom: 16px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 5px; background: var(--surface-2); font: 700 12px ui-monospace, SFMono-Regular, Consolas, monospace; overflow-wrap: anywhere; }
.detail-summary { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.detail-summary div { padding: 12px; border-right: 1px solid var(--line); }
.detail-summary div:last-child { border-right: 0; }
.detail-summary span, .detail-summary strong { display: block; }
.detail-summary span { color: var(--muted); font-size: 11px; }
.detail-summary strong { margin-top: 3px; font-size: 16px; }
.detail-section { margin-top: 20px; }
.detail-section h3 { margin: 0 0 8px; font-size: 13px; }
.detail-section table { min-width: 680px; border: 1px solid var(--line); }

.toast { position: fixed; z-index: 50; right: 22px; bottom: 22px; max-width: min(420px, calc(100% - 32px)); padding: 11px 15px; border-radius: 5px; background: var(--ink); color: #fff; box-shadow: var(--shadow); }
.toast.error { background: #8f2f36; }

@media (max-width: 1040px) {
  .metrics { grid-template-columns: repeat(3, 1fr); }
  .metrics article { border-bottom: 1px solid var(--line); }
  .metrics article:nth-child(3) { border-right: 0; }
  .metrics article:nth-child(n+4) { border-bottom: 0; }
  .workspace { grid-template-columns: 1fr; }
  .generator form { grid-template-columns: repeat(3, minmax(0, 1fr)); align-items: end; }
  .generator .wide { width: auto; }
}

@media (max-width: 760px) {
  .topbar { height: 58px; padding: 0 12px; }
  .sync-state, .top-actions .ghost span, .brand small { display: none; }
  .top-actions .ghost { width: 38px; padding: 0; }
  .admin-main { width: calc(100% - 20px); margin-top: 18px; }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .metrics article, .metrics article:nth-child(3), .metrics article:nth-child(n+4) { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
  .metrics article:nth-child(even) { border-right: 0; }
  .metrics article:last-child { grid-column: 1 / -1; border-right: 0; border-bottom: 0; }
  .generator form { grid-template-columns: 1fr; }
  .directory-head { align-items: stretch; flex-direction: column; }
  .directory-tools { align-items: stretch; flex-direction: column; }
  .search { width: 100%; }
  .segments { width: 100%; }
  .segments button { flex: 1; min-width: 0; }
  .directory .table-wrap { overflow: visible; }
  .directory table { min-width: 0; }
  .directory thead { display: none; }
  .directory tbody { display: block; }
  .directory tbody tr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 18px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
  .directory tbody td { display: grid; min-width: 0; gap: 3px; padding: 0; border: 0; }
  .directory tbody td::before { content: attr(data-label); color: var(--muted); font-size: 10px; font-weight: 800; }
  .directory tbody td:first-child, .directory tbody td:last-child, .directory tbody .empty { grid-column: 1 / -1; }
  .directory tbody .empty { padding: 28px 0; }
  .directory .code-cell { justify-content: space-between; }
  .directory .status { justify-self: start; }
  .directory .note-cell { max-width: none; }
  .directory .row-actions { justify-content: flex-start; }
  .detail-summary { grid-template-columns: repeat(2, 1fr); }
  .detail-summary div:nth-child(2) { border-right: 0; }
  .detail-summary div:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .login-panel { padding: 26px 22px; }
}
