:root {
  --ink: #151719;
  --muted: #687078;
  --line: #d9dde0;
  --paper: #f4f5f3;
  --white: #ffffff;
  --surface-muted: #eef0ed;
  --field: #ffffff;
  --table-head: #f7f8f6;
  --table-line: #eceeed;
  --subtle-code: #f6f7f5;
  --nav: #111416;
  --green: #1f7657;
  --green-soft: #dfeee8;
  --blue: #2864b3;
  --amber: #b56c22;
  --red: #b03a3a;
  --shadow: 0 16px 40px rgba(19, 25, 29, .08);
  --on-dark: #f7faf8;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--paper);
  font-synthesis: none;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e8ecea;
  --muted: #9aa5a0;
  --line: #343c38;
  --paper: #171b19;
  --white: #202623;
  --surface-muted: #131715;
  --field: #181d1b;
  --table-head: #1a201d;
  --table-line: #303733;
  --subtle-code: #171c19;
  --nav: #0d100f;
  --green: #58b58a;
  --red: #ef7777;
  --shadow: 0 16px 40px rgba(0, 0, 0, .24);
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 0; min-height: 100vh; background: var(--paper); }
button, input, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.eyebrow { margin: 0 0 8px; color: var(--green); font-size: 11px; font-weight: 800; letter-spacing: 1.7px; }
.muted { color: var(--muted); line-height: 1.6; }
.compact-copy { margin: 7px 0 0; font-size: 12px; }
code { font-family: "SFMono-Regular", Consolas, monospace; }

.auth-shell { min-height: 100vh; display: grid; grid-template-columns: minmax(360px, 1.15fr) minmax(360px, .85fr); }
.auth-brand { padding: clamp(48px, 8vw, 120px); color: var(--on-dark); background: #101416; display: flex; flex-direction: column; justify-content: center; position: relative; overflow: hidden; }
.auth-brand::after { content: ""; position: absolute; right: -90px; bottom: -90px; width: 360px; height: 360px; border: 1px solid #355247; transform: rotate(18deg); }
.auth-brand h1 { margin: 8px 0 20px; font-family: Georgia, "Times New Roman", serif; font-size: clamp(56px, 8vw, 116px); font-weight: 400; line-height: .9; }
.auth-copy { max-width: 590px; color: #bec8c3; font-size: 19px; line-height: 1.6; }
.brand-mark { width: 58px; height: 58px; display: grid; place-items: center; margin-bottom: 42px; border: 1px solid #4b725f; color: #91d2b5; font-weight: 800; }
.brand-mark.small { width: 38px; height: 38px; margin: 0; font-size: 12px; }
.signal-row { display: flex; flex-wrap: wrap; gap: 22px; margin-top: 38px; color: #aeb8b3; font-size: 13px; }
.signal-row span { display: flex; align-items: center; gap: 8px; }
.signal-row i { width: 7px; height: 7px; background: #54b487; border-radius: 50%; }
.auth-panel { padding: 48px; display: grid; place-items: center; background: var(--surface-muted); }
.auth-form { width: min(430px, 100%); padding: 48px; background: var(--white); border: 1px solid var(--line); box-shadow: var(--shadow); display: grid; gap: 22px; }
.auth-form h2 { margin: 0; font: 400 38px/1.1 Georgia, serif; }
label { display: grid; gap: 8px; color: var(--ink); font-size: 13px; font-weight: 700; }
input, select { width: 100%; min-height: 44px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 4px; background: var(--field); color: var(--ink); outline: none; }
input:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(31, 118, 87, .12); }
button:disabled { cursor: wait; opacity: .58; }
.btn { min-height: 42px; padding: 10px 17px; border: 1px solid transparent; border-radius: 4px; font-weight: 750; }
.btn.primary { color: var(--on-dark); background: var(--green); }
.btn.primary:hover { background: #195f47; }
.btn.secondary { border-color: var(--line); background: var(--white); color: var(--ink); }
.btn.danger { border-color: color-mix(in srgb, var(--red) 40%, var(--line)); color: var(--red); background: color-mix(in srgb, var(--red) 7%, var(--white)); }
.btn.compact { min-height: 34px; padding: 6px 10px; font-size: 12px; }
.text-btn { border: 0; background: transparent; color: var(--green); font-weight: 750; }
.text-btn.left { padding: 8px 0; text-align: left; color: #9faaa5; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 240px 1fr; }
.sidebar { position: sticky; top: 0; height: 100vh; padding: 28px 18px; display: flex; flex-direction: column; color: #e8eeeb; background: var(--nav); }
.logo-row { display: flex; align-items: center; gap: 12px; padding: 0 8px 36px; }
.sidebar nav { display: grid; gap: 5px; }
.nav-item { width: 100%; min-height: 44px; padding: 10px 12px; border: 0; border-left: 2px solid transparent; background: transparent; color: #aab4af; display: flex; align-items: center; gap: 13px; text-align: left; font-weight: 680; }
.nav-item span { color: #606b66; font-size: 10px; }
.nav-item:hover, .nav-item.active { color: var(--on-dark); background: #1b211e; border-left-color: #58a981; }
.sidebar-footer { margin-top: auto; padding: 16px 12px 0; border-top: 1px solid #2c3430; display: grid; gap: 6px; overflow: hidden; }
.sidebar-footer span { color: #c6cfca; font-size: 12px; text-overflow: ellipsis; overflow: hidden; }
.workspace { min-width: 0; padding: 32px clamp(20px, 4vw, 64px) 60px; }
.topbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 30px; }
.topbar h1 { min-width: 0; margin: 0; overflow-wrap: anywhere; font: 400 40px/1 Georgia, serif; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.theme-toggle { width: 44px; height: 44px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: var(--white); color: var(--ink); font-size: 20px; }
.auth-theme-toggle { position: fixed; z-index: 30; top: 20px; right: 20px; }
.balance-box { min-width: 170px; padding: 11px 14px; border-left: 3px solid var(--green); background: var(--white); display: grid; gap: 2px; }
.balance-box span { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.balance-box strong { font-size: 19px; }
.tab-page { min-width: 0; display: grid; gap: 22px; }
.tab-page > * { min-width: 0; }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--line); background: var(--white); }
.admin-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.metrics article { min-height: 130px; padding: 23px; border-right: 1px solid var(--line); display: flex; flex-direction: column; justify-content: space-between; }
.metrics article:last-child { border: 0; }
.metrics span, .metrics small { color: var(--muted); font-size: 12px; }
.metrics strong { font: 400 31px/1 Georgia, serif; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.panel { padding: 25px; border: 1px solid var(--line); background: var(--white); box-shadow: 0 6px 22px rgba(28, 34, 37, .035); overflow: hidden; }
.endpoint-panel { display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.endpoint-panel code { display: block; overflow-wrap: anywhere; font-size: 14px; color: var(--ink); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.section-head h2 { margin: 0; font: 400 27px/1.15 Georgia, serif; }
.search-input { width: min(300px, 100%); }
.catalog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.table-wrap { width: 100%; overflow-x: auto; }
.pagination { min-height: 42px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--table-line); display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.pagination-info { min-width: 210px; color: var(--muted); font-size: 12px; text-align: center; }
.pagination-btn { flex: 0 0 36px; font-size: 18px; }
.pagination-btn:disabled { cursor: default; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { padding: 11px 12px; color: var(--muted); border-bottom: 1px solid var(--line); background: var(--table-head); text-align: left; font-size: 10px; letter-spacing: .8px; text-transform: uppercase; white-space: nowrap; }
td { padding: 13px 12px; border-bottom: 1px solid var(--table-line); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
.cell-main { font-weight: 720; }
.cell-sub { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }
.key-actions { display: flex; gap: 7px; min-width: 178px; }
.tariff-cell { display: inline-block; min-width: 150px; line-height: 1.65; white-space: nowrap; }
.status { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.status.off::before, .status.error::before { background: var(--red); }
.empty { padding: 34px 0; color: var(--muted); text-align: center; }
.positive { color: var(--green); }
.negative { color: var(--red); }
.inline-form { display: flex; flex-wrap: wrap; gap: 13px; align-items: end; }
.multiplier-form label { flex: 1 1 200px; }
.multiplier-form button { flex: 0 0 auto; }
.promo-redeem-form label { flex: 1 1 260px; }
.promo-form label { flex: 1 1 150px; }
.promo-form button { flex: 0 0 auto; }
.role-select { min-width: 150px; min-height: 36px; padding-block: 6px; }
.spaced-table { margin-top: 22px; }
.service-strip { margin: 0 0 22px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border: 1px solid var(--line); }
.service-strip > div { min-height: 58px; padding: 11px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px; border-right: 1px solid var(--line); }
.service-strip > div:last-child { border-right: 0; }
.service-strip small { color: var(--muted); white-space: nowrap; }
.mapping-form { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)); gap: 14px; align-items: end; }
.provider-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 22px; }
.provider-card { display: grid; gap: 11px; padding: 16px; border: 1px solid var(--line); background: var(--surface-muted); }
.provider-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.provider-card .btn { width: 100%; }
.usage-chart { display: grid; gap: 17px; }
.usage-chart-row { display: grid; gap: 7px; }
.usage-chart-label { display: flex; justify-content: space-between; gap: 14px; font-size: 13px; }
.usage-chart-label span { color: var(--muted); white-space: nowrap; }
.usage-chart-track { height: 12px; overflow: hidden; background: var(--surface-muted); border: 1px solid var(--line); }
.usage-chart-track span { display: block; height: 100%; min-width: 2px; background: var(--green); }
.mapping-form .toggle-row { min-height: 44px; padding: 0 4px; }
.mapping-actions { display: flex; align-items: center; gap: 8px; }
.mapping-actions .btn { flex: 1; }
.formula { margin-top: 20px; padding: 16px; border-left: 3px solid var(--green); background: var(--subtle-code); overflow-x: auto; }
dialog { width: min(500px, calc(100vw - 32px)); padding: 27px; border: 1px solid var(--line); border-radius: 5px; background: var(--white); color: var(--ink); box-shadow: 0 24px 70px rgba(0,0,0,.24); }
dialog.wide-dialog { width: min(840px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow-y: auto; }
dialog::backdrop { background: rgba(11, 15, 16, .65); }
dialog form { display: grid; gap: 17px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 15px; margin-bottom: 17px; }
.dialog-head h2 { margin: 0; font: 400 27px/1 Georgia, serif; }
.icon-btn { width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 4px; background: var(--white); font-size: 24px; line-height: 1; }
.secret-box { display: grid; gap: 13px; }
.secret-box code { padding: 14px; border: 1px solid var(--line); background: var(--subtle-code); overflow-wrap: anywhere; line-height: 1.6; }
.use-key-options { display: grid; grid-template-columns: minmax(220px, 1.25fr) minmax(240px, 1fr); gap: 17px; margin-bottom: 22px; }
.use-key-options .full-row { grid-column: 1 / -1; }
.control-group { min-width: 0; margin: 0; padding: 0; border: 0; }
.control-group legend { margin-bottom: 8px; color: var(--ink); font-size: 13px; font-weight: 700; }
.segmented { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); min-height: 44px; border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
.segmented label { min-width: 0; display: block; color: var(--muted); font-weight: 700; }
.segmented label + label { border-left: 1px solid var(--line); }
.segmented input { position: absolute; width: 1px; height: 1px; min-height: 0; opacity: 0; }
.segmented span { height: 42px; padding: 0 10px; display: grid; place-items: center; text-align: center; }
.segmented input:checked + span { color: var(--on-dark); background: var(--green); }
.toggle-row { min-height: 44px; display: flex; align-items: center; gap: 10px; align-self: end; }
.toggle-row input { width: 18px; min-height: 18px; height: 18px; accent-color: var(--green); }
.toggle-row.disabled { color: var(--muted); opacity: .55; }
.config-section { margin-top: 18px; }
.config-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 8px; }
.config-head > div { min-width: 0; display: grid; gap: 3px; }
.config-head strong { overflow-wrap: anywhere; }
.config-head code { color: var(--muted); font-size: 11px; overflow-wrap: anywhere; }
.config-section pre { min-height: 118px; max-height: 280px; margin: 0; padding: 15px; overflow: auto; border: 1px solid var(--line); border-radius: 4px; background: var(--subtle-code); color: var(--ink); font-size: 12px; line-height: 1.55; white-space: pre; }
.toast { position: fixed; z-index: 100; top: 20px; right: 20px; max-width: 390px; padding: 13px 17px; border-left: 4px solid var(--green); background: #18201c; color: var(--on-dark); box-shadow: var(--shadow); opacity: 0; pointer-events: none; transform: translateY(-12px); transition: .18s ease; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { border-left-color: #dc5757; }

@media (max-width: 950px) {
  .auth-shell { grid-template-columns: 1fr; }
  .auth-brand { min-height: 42vh; padding: 45px 30px; }
  .auth-brand h1 { font-size: 58px; }
  .auth-brand .brand-mark { margin-bottom: 24px; }
  .auth-panel { padding: 28px 18px; }
  .app-shell { grid-template-columns: 76px 1fr; }
  .sidebar { padding-inline: 10px; }
  .logo-row strong, .nav-item:not(.active)::after, .nav-item { font-size: 0; }
  .logo-row { padding-inline: 8px; }
  .nav-item { justify-content: center; gap: 0; }
  .nav-item span { font-size: 10px; }
  .sidebar-footer span { display: none; }
  .text-btn.left { font-size: 11px; text-align: center; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .admin-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .mapping-form { grid-template-columns: repeat(2, minmax(180px, 1fr)); }
  .provider-grid { grid-template-columns: 1fr; }
  .metrics article:nth-child(2) { border-right: 0; }
  .metrics article:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .admin-metrics article:nth-child(2) { border-right: 1px solid var(--line); }
  .admin-metrics article:nth-child(-n+2) { border-bottom: 0; }
}

@media (max-width: 620px) {
  .auth-form { padding: 29px 22px; }
  .signal-row { display: none; }
  .app-shell { display: block; padding-bottom: 68px; }
  .sidebar { position: fixed; z-index: 20; inset: auto 0 0 0; width: 100%; height: 62px; padding: 6px 8px; flex-direction: row; border-top: 1px solid #303833; }
  .sidebar .logo-row, .sidebar-footer { display: none; }
  .sidebar nav { width: 100%; grid-auto-flow: column; grid-auto-columns: 1fr; }
  .nav-item { min-height: 48px; border-left: 0; border-bottom: 2px solid transparent; }
  .nav-item.active { border-bottom-color: #58a981; border-left-color: transparent; }
  .workspace { padding: 22px 14px 36px; }
  .topbar h1 { font-size: 32px; }
  .balance-box { min-width: 138px; }
  .metrics article { min-height: 112px; padding: 17px; }
  .metrics strong { font-size: 24px; }
  .panel { padding: 18px; }
  .endpoint-panel, .section-head { align-items: flex-start; }
  .section-head { flex-direction: column; }
  .section-head > .btn { width: 100%; }
  .admin-metrics { grid-template-columns: 1fr; }
  .admin-metrics article,
  .admin-metrics article:nth-child(2),
  .admin-metrics article:nth-child(-n+2) { border-right: 0; border-bottom: 1px solid var(--line); }
  .admin-metrics article:last-child { border-bottom: 0; }
  .pagination { justify-content: center; }
  .pagination-info { min-width: 0; flex: 1; }
  .endpoint-panel { flex-direction: column; }
  .endpoint-panel .btn { width: 100%; }
  .use-key-options { grid-template-columns: 1fr; }
  .key-actions { min-width: 0; }
  .service-strip, .mapping-form { grid-template-columns: 1fr; }
  .usage-chart-label { align-items: flex-start; flex-direction: column; gap: 3px; }
  .service-strip > div { border-right: 0; border-bottom: 1px solid var(--line); }
  .service-strip > div:last-child { border-bottom: 0; }
}

@media (max-width: 380px) {
  .topbar { align-items: stretch; flex-direction: column; }
  .topbar-actions { width: 100%; }
  .balance-box { min-width: 0; flex: 1; }
  dialog, dialog.wide-dialog { width: calc(100vw - 16px); padding: 20px; }
  .config-head { align-items: stretch; flex-direction: column; gap: 9px; }
  .config-head .btn { width: 100%; }
}
