:root {
  color-scheme: light dark;
  --bg: #f5f6f8; --surface: #ffffff; --surface-2: #f0f2f5; --border: #e2e5ea;
  --text: #171a21; --muted: #667085;
  --primary: #4f46e5; --primary-hover: #4338ca; --on-primary: #ffffff;
  --danger: #c62828; --danger-bg: #fdecec; --ok: #137a4b; --ok-bg: #e7f6ee; --warn: #8a5300; --warn-bg: #fff3d9;
  --radius: 12px; --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1218; --surface: #161b23; --surface-2: #1e2530; --border: #28313f;
    --text: #e8ecf2; --muted: #97a1b2;
    --primary: #7c83ff; --primary-hover: #959bff; --on-primary: #0b0e1a;
    --danger: #ff9494; --danger-bg: #3a1f26; --ok: #6fd6a0; --ok-bg: #153329; --warn: #f3c76a; --warn-bg: #382e14;
    --shadow: none;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2, p { margin: 0; }
h2 { font-size: 17px; font-weight: 650; }
p { margin-top: 4px; }
code { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Topbar & tabs */
.topbar { position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1040px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-name { font-weight: 700; font-size: 16px; }
.logo { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: var(--on-primary); font-size: 12px; font-weight: 800; letter-spacing: .5px; }
.brand-lg .logo { width: 38px; height: 38px; font-size: 14px; }
.brand-lg .brand-name { font-size: 20px; }
.tabs { max-width: 1040px; margin: 0 auto; padding: 0 12px; display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { flex: none; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600; padding: 10px 14px; border-bottom: 2px solid transparent; cursor: pointer; }
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--primary); border-bottom-color: var(--primary); }

/* Layout */
.container { max-width: 1040px; margin: 0 auto; padding: 20px max(16px, env(safe-area-inset-right)) calc(48px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
.panel { display: grid; gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }

/* Ringkasan di atas dashboard: angka kunci sekilas, tanpa menu baru. */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; display: grid; gap: 1px; }
.stat b { font-size: 20px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); font-weight: 600; }
.stat .dot-ok { color: var(--ok); }
.stat .dot-off { color: var(--muted); }
.stack { display: grid; gap: 14px; }
.narrow { max-width: 420px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; align-items: end; margin-bottom: 8px; }
.grid.two { grid-template-columns: minmax(0, 1fr) auto; }
.field { display: grid; gap: 6px; min-width: 0; }
.field.end { align-self: end; }
label { font-size: 13px; font-weight: 600; color: var(--muted); }
.hint { font-size: 12.5px; }

/* Controls */
input, select, textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); color: var(--text); font: inherit; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 45%, transparent); outline-offset: 0; border-color: var(--primary); }
textarea { resize: vertical; min-height: 110px; font-family: var(--mono); font-size: 13.5px; line-height: 1.5; }
textarea.tall { min-height: 320px; }
.counter { text-align: right; font-size: 12.5px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid transparent; border-radius: 9px; padding: 9px 15px; font: inherit; font-weight: 600; cursor: pointer; background: var(--surface-2); color: var(--text); }
.btn:hover { background: var(--border); }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.danger { background: var(--danger-bg); color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; color: var(--text); font-size: 15px; font-weight: 600; width: fit-content; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { position: relative; width: 40px; height: 22px; border-radius: 99px; background: var(--border); transition: background .15s; }
.switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + .track { background: var(--primary); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Tables */
.table-wrap { overflow-x: auto; margin-top: 12px; border: 1px solid var(--border); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
th { background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }
td.mono { font-family: var(--mono); font-size: 13px; word-break: break-all; }
td.nowrap { white-space: nowrap; }
td.actions { text-align: right; white-space: nowrap; }
td.actions .btn + .btn { margin-left: 6px; }
td.empty { text-align: center; color: var(--muted); padding: 22px 12px; }

.pill { display: inline-block; font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); padding: 3px 10px; border-radius: 99px; white-space: nowrap; }

/* Statistik penggunaan */
.ranges { display: flex; gap: 2px; background: var(--surface-2); padding: 4px; border-radius: 10px; flex: none; }
.range { border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600; font-size: 13.5px; padding: 6px 12px; border-radius: 7px; cursor: pointer; white-space: nowrap; }
.range:hover { color: var(--text); }
.range[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.usage-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.usage-card { border: 1px solid var(--border); border-radius: 12px; padding: 16px 12px; display: grid; gap: 6px; align-content: start; text-align: center; }
.usage-label { font-size: 11.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.usage-card b { font-size: 24px; font-weight: 750; font-variant-numeric: tabular-nums; word-break: break-all; line-height: 1.15; }
.usage-sub { font-size: 12px; color: var(--muted); }
.c-orange { color: #ea580c; }
.c-blue { color: #2563eb; }
.c-green { color: #059669; }
.c-amber { color: #d97706; }
@media (prefers-color-scheme: dark) {
  .c-orange { color: #fb923c; }
  .c-blue { color: #60a5fa; }
  .c-green { color: #34d399; }
  .c-amber { color: #fbbf24; }
}
.card-head { flex-wrap: wrap; }

/* One-time key */
.keybox { margin: 4px 0 16px; padding: 14px; border: 1px solid var(--ok); background: var(--ok-bg); border-radius: 10px; display: grid; gap: 10px; }
.keybox p { margin: 0; color: var(--text); font-size: 14px; }
.keyrow { display: flex; gap: 8px; }
.keyrow input { font-family: var(--mono); font-size: 13px; }
.keybox .btn.ghost { justify-self: start; }

/* Banner & toast */
.banner { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 14px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 50; max-width: calc(100vw - 32px); padding: 11px 16px; border-radius: 10px; background: var(--text); color: var(--bg); font-size: 14px; font-weight: 550; box-shadow: 0 6px 24px rgba(0, 0, 0, .25); }
.toast.ok { background: var(--ok); color: #fff; }
.toast.error { background: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .toast.ok, .toast.error { color: #0b0e14; } }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 400px; display: grid; gap: 14px; }
.login-card .brand { margin-bottom: 6px; }

footer { margin-top: 24px; }
.sr-copy { position: fixed; top: 0; left: 0; width: 1px; height: 1px; opacity: 0; font-size: 16px; }

@media (max-width: 600px) {
  .grid.two { grid-template-columns: 1fr; }
  .keyrow { flex-direction: column; }
  .toast { bottom: calc(14px + env(safe-area-inset-bottom)); }
}

/* Kelola API key */
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.key-toolbar { display: flex; gap: 8px; flex-wrap: wrap; }
.key-list { display: grid; gap: 8px; max-height: 480px; overflow-y: auto; padding-right: 2px; }
.key-row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.key-no { min-width: 22px; text-align: center; font-size: 12px; font-weight: 700; color: var(--muted); }
.key-main { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; min-width: 0; }
.key-preview { font-size: 13.5px; word-break: break-all; }
.key-msg { flex-basis: 100%; }
.key-actions { display: flex; gap: 6px; }
.empty-note { text-align: center; padding: 18px 8px; border: 1px dashed var(--border); border-radius: 10px; }
.badge { display: inline-block; font-size: 12px; font-weight: 650; padding: 2px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge.valid { background: var(--ok-bg); color: var(--ok); }
.badge.invalid { background: var(--danger-bg); color: var(--danger); }
.badge.limited, .badge.unknown { background: var(--warn-bg); color: var(--warn); }
.badge.error { background: var(--danger-bg); color: var(--danger); }
.badge.checking { background: var(--surface-2); color: var(--muted); }

/* Tablet */
@media (max-width: 900px) {
  .usage-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
}

/* HP: tabel jadi kartu bertumpuk supaya tidak perlu geser samping */
@media (max-width: 640px) {
  body { font-size: 15px; }
  input, select, textarea { font-size: 16px; } /* cegah auto-zoom iOS */
  .btn { min-height: 40px; }
  .btn.small { min-height: 36px; padding: 6px 12px; }
  .card { padding: 14px; }
  .topbar-inner { padding: 8px 12px; }
  .tab { padding: 10px 12px; }
  .grid { grid-template-columns: 1fr; }
  .grid .btn, .btn-row .btn, #bulk-keys-form .btn { width: 100%; }
  .key-toolbar .btn { flex: 1 1 140px; }
  .ranges { width: 100%; overflow-x: auto; }
  .range { flex: 1 0 auto; }

  .table-wrap { border: 0; overflow: visible; margin-top: 12px; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }
  tbody tr { border: 1px solid var(--border); border-radius: 10px; padding: 6px 12px; margin-bottom: 10px; background: var(--surface); }
  tbody tr:last-child { margin-bottom: 0; }
  td { display: grid; grid-template-columns: 96px minmax(0, 1fr); gap: 10px; align-items: start; padding: 7px 0; border-bottom: 1px solid var(--border); text-align: left; }
  td:last-child { border-bottom: 0; }
  td[data-label]::before { content: attr(data-label); font-size: 12px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; padding-top: 2px; }
  td:not([data-label]) { grid-template-columns: 1fr; }
  td.actions { display: flex; justify-content: flex-end; gap: 8px; padding-top: 10px; }
  td.actions .btn { flex: 1; margin: 0; }
  td.actions .btn + .btn { margin-left: 0; }
  td.empty { display: block; text-align: center; }

  .key-row { grid-template-columns: minmax(0, 1fr) auto; gap: 6px 10px; padding: 10px; }
  .key-no { display: none; }
  .key-main { flex-wrap: wrap; }
  .key-actions .btn { flex: none; padding: 6px 12px; }
}

/* Daftar provider + panel key per provider */
.prov-list { display: grid; gap: 10px; margin-top: 12px; }
.prov { border: 1px solid var(--border); border-radius: 12px; padding: 12px; display: grid; gap: 10px; background: var(--surface); }
.prov-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; flex-wrap: wrap; }
.prov-info { display: grid; gap: 2px; min-width: 0; flex: 1 1 220px; }
.prov-info strong { font-size: 16px; }
.prov-url { font-size: 12.5px; color: var(--muted); word-break: break-all; }
.prov-actions { display: flex; gap: 6px; }
.prov-toggle { justify-content: space-between; width: 100%; }
.chev { color: var(--muted); }
.key-panel { display: grid; gap: 10px; padding-top: 10px; border-top: 1px dashed var(--border); }
.key-panel .key-row { background: var(--surface-2); border-color: transparent; }
@media (max-width: 640px) {
  .prov-actions { width: 100%; }
  .prov-actions .btn { flex: 1; }
  .key-toolbar .btn { flex: 1 1 120px; }
}

.pill.proxy { max-width: 100%; overflow-wrap: anywhere; font-family: ui-monospace, monospace; font-size: 11.5px; }
.pill.proxy.on { background: var(--ok-bg); color: var(--ok); }
.key-actions { flex-wrap: wrap; }
@media (max-width: 640px) {
  .key-row { grid-template-columns: minmax(0, 1fr); }
  .key-actions { width: 100%; }
  .key-actions .btn { flex: 1 1 0; }
}
