:root {
  color-scheme: light;
  --bg: oklch(0.975 0.006 238);
  --panel: oklch(0.997 0.003 238);
  --panel-2: oklch(0.955 0.008 238);
  --panel-3: oklch(0.925 0.01 238);
  --ink: oklch(0.22 0.018 245);
  --muted: oklch(0.5 0.018 245);
  --faint: oklch(0.63 0.015 245);
  --line: oklch(0.875 0.012 245);
  --line-soft: oklch(0.925 0.009 245);
  --accent: oklch(0.52 0.14 218);
  --accent-soft: oklch(0.93 0.045 218);
  --accent-ink: oklch(0.985 0.006 218);
  --good: oklch(0.52 0.13 153);
  --good-soft: oklch(0.93 0.055 153);
  --warn: oklch(0.6 0.13 72);
  --warn-soft: oklch(0.94 0.06 80);
  --bad: oklch(0.55 0.18 28);
  --bad-soft: oklch(0.94 0.055 28);
  --shadow: 0 14px 34px oklch(0.2 0.02 245 / 0.08);
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
code, pre { font-family: var(--mono); }
.muted { color: var(--faint); font-size: 12px; }
.note { color: var(--muted); font-size: 12.5px; line-height: 1.55; }
.error-text { color: var(--bad); font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  outline: none;
  transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
}
input, select { min-height: 36px; padding: 0 10px; }
textarea { min-height: 192px; padding: 10px; resize: vertical; font-family: var(--mono); font-size: 12px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px oklch(0.52 0.14 218 / 0.16); }
input::placeholder, textarea::placeholder { color: var(--faint); }
select { padding-right: 28px; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  font-weight: 650;
  cursor: pointer;
  white-space: nowrap;
  transition: background 140ms ease, border-color 140ms ease, opacity 140ms ease;
}
.button:hover { background: var(--panel-2); }
.button:disabled { opacity: 0.45; cursor: not-allowed; }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.button.primary:hover { background: oklch(0.48 0.145 218); }
.button.subtle { background: transparent; }
.button.small { min-height: 30px; padding: 0 10px; font-size: 12px; }
.button.icon { width: 36px; padding: 0; font-size: 18px; }

.gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.gate-card {
  width: min(420px, 100%);
  display: grid;
  gap: 15px;
  padding: 26px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.gate-mark, .brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 800;
  letter-spacing: 0;
}
.gate-card h1 { font-size: 22px; line-height: 1.2; }
.gate-card p { margin-top: 3px; }
.field { display: grid; gap: 7px; }
.field span { color: var(--muted); font-size: 12px; font-weight: 700; }

.app { width: min(1680px, 100%); margin: 0 auto; padding: 0 18px 40px; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 0;
  background: oklch(0.975 0.006 238 / 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line-soft);
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 230px; }
.brand strong { display: block; font-size: 15px; }
.brand span { display: block; max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--faint); font-size: 12px; }
.top-status { display: flex; align-items: center; gap: 8px; margin-left: auto; color: var(--muted); font-size: 12.5px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.dot.ok { background: var(--good); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.top-actions select { width: 98px; }
.scope-select {
  display: grid;
  gap: 3px;
  width: 178px;
}
.scope-select span {
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 800;
}
.scope-select select { width: 100%; min-height: 34px; }
.switch {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
.switch input, .check input { width: auto; min-height: 0; }

.tabs { display: flex; flex-wrap: wrap; gap: 6px; padding: 14px 0 0; }
.tab {
  min-height: 34px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  cursor: pointer;
}
.tab:hover { background: var(--panel); }
.tab.active { background: var(--panel); border-color: var(--line); color: var(--accent); }
.badge {
  margin-left: 5px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--bad-soft);
  color: var(--bad);
  font-size: 11px;
}

.content { padding-top: 16px; }
.panel { display: none; }
.panel.active { display: grid; gap: 16px; }
.layout { display: grid; gap: 16px; }
.layout.two { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.oauth-layout { grid-template-columns: minmax(320px, 0.82fr) minmax(0, 1.18fr); }
.block {
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 1px 1px oklch(0.2 0.02 245 / 0.03);
}
.block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.block-head h2 { font-size: 15px; font-weight: 800; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
}
.kpi {
  min-height: 92px;
  padding: 13px;
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
}
.kpi span { display: block; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 700; }
.kpi .value { margin-top: 8px; font-size: 24px; line-height: 1; font-weight: 820; font-variant-numeric: tabular-nums; }
.kpi .sub { margin-top: 8px; color: var(--faint); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi.good .value { color: var(--good); }
.kpi.warn .value { color: var(--warn); }
.kpi.bad .value { color: var(--bad); }

.status-grid, .kv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.status-item, .kv {
  padding: 12px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel-2);
}
.status-item b, .kv b { display: block; font-size: 13px; }
.status-item span, .kv span { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }

.breakdown, .profile-mini, .profile-list, .problem-list { display: grid; gap: 8px; }
.break-row, .profile-row, .problem-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel-2);
}
.break-row strong, .profile-row strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.break-row span, .profile-row span, .problem-row span { color: var(--muted); font-size: 12px; }
.bar { height: 5px; margin-top: 7px; border-radius: 999px; background: var(--panel-3); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: inherit; background: var(--accent); }

.profile-row { grid-template-columns: minmax(150px, 0.8fr) minmax(0, 1.1fr) auto auto auto; }
.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--panel-3);
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
  white-space: nowrap;
}
.pill.ok { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.active { background: var(--accent-soft); color: var(--accent); }

.stack { display: grid; gap: 12px; }
.actions-row { display: flex; gap: 8px; flex-wrap: wrap; }
.create-key-form {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) 94px minmax(150px, 0.8fr) auto;
  gap: 8px;
  align-items: end;
  width: min(760px, 100%);
}
.compact-field { display: grid; gap: 5px; }
.compact-field span { color: var(--muted); font-size: 11px; font-weight: 800; }
.rpm-field input { text-align: right; }
.toolbar, .request-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.toolbar input { max-width: 360px; }
.check { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; font-weight: 700; white-space: nowrap; }
.created-key {
  margin-bottom: 12px;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--accent-soft);
  background: var(--accent-soft);
}
.created-key-inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}
.created-key code { display: block; margin-top: 6px; overflow-wrap: anywhere; }
.created-key .muted { display: block; margin-top: 2px; }

.chips, .filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.chip {
  min-height: 32px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--muted);
  font-weight: 700;
  cursor: pointer;
}
.chip.active { background: var(--accent-soft); border-color: var(--accent-soft); color: var(--accent); }
.filters select { width: 150px; }
.filters input { width: 150px; }

.table-wrap { overflow: auto; border: 1px solid var(--line-soft); border-radius: var(--radius); }
.data-table { width: 100%; border-collapse: collapse; min-width: 980px; }
.data-table th, .data-table td { padding: 10px; border-bottom: 1px solid var(--line-soft); vertical-align: top; }
.data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 12px;
  text-align: left;
}
.data-table th.num { text-align: right; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table code { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.data-table input, .data-table select { min-height: 32px; }
.actions-col { width: 172px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.preview { max-width: 360px; color: var(--muted); font-size: 12px; }
.empty { padding: 18px; color: var(--muted); text-align: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 50;
  transform: translateX(-50%);
  max-width: min(560px, 92vw);
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  font-weight: 700;
}
.toast.ok { background: var(--good-soft); border-color: var(--good-soft); }
.toast.error { background: var(--bad-soft); border-color: var(--bad-soft); }

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: oklch(0.18 0.02 245 / 0.25);
}
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 70;
  width: min(560px, 100vw);
  height: 100vh;
  padding: 22px;
  background: var(--panel);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: auto;
}
.drawer h2 { font-size: 18px; margin-right: 42px; }
.drawer-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  cursor: pointer;
}
.drawer-body { display: grid; gap: 10px; margin-top: 18px; }
.detail-item { padding: 10px; border: 1px solid var(--line-soft); border-radius: var(--radius); background: var(--panel-2); }
.detail-item span { display: block; color: var(--muted); font-size: 12px; font-weight: 700; }
.detail-item pre { margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; }

@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .layout.two, .oauth-layout { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
  .top-status { margin-left: 0; }
  .top-actions { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
  .scope-select { width: min(220px, 100%); }
}
@media (max-width: 720px) {
  .app { padding-inline: 12px; }
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .status-grid, .kv-grid { grid-template-columns: 1fr; }
  .block { padding: 13px; }
  .block-head, .toolbar, .request-toolbar { align-items: stretch; flex-direction: column; }
  .create-key-form { grid-template-columns: 1fr; }
  .created-key-inner { grid-template-columns: 1fr; }
  .filters select, .filters input, .toolbar input { width: 100%; max-width: none; }
  .scope-select { width: 100%; }
  .profile-row { grid-template-columns: 1fr; }
}
