:root {
  color-scheme: light;
  --page: oklch(97% 0.014 225);
  --surface: oklch(99% 0.006 225);
  --surface-soft: oklch(95.5% 0.025 228);
  --ink: oklch(22% 0.04 248);
  --text: oklch(33% 0.032 245);
  --muted: oklch(54% 0.03 242);
  --line: oklch(87% 0.03 228);
  --line-strong: oklch(77% 0.06 232);
  --primary: oklch(62% 0.19 236);
  --primary-strong: oklch(52% 0.21 244);
  --primary-soft: oklch(93% 0.045 230);
  --success: oklch(61% 0.14 164);
  --warning: oklch(73% 0.15 78);
  --danger: oklch(60% 0.19 28);
  --shadow: 0 14px 34px oklch(40% 0.08 240 / 11%);
  --radius: 8px;
  --sidebar-width: 248px;
  --ease: 190ms cubic-bezier(0.22, 1, 0.36, 1);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 320px; min-height: 100%; }
body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background-color: var(--page);
  background-image:
    linear-gradient(90deg, oklch(76% 0.07 232 / 8%) 1px, transparent 1px),
    linear-gradient(0deg, oklch(76% 0.07 232 / 7%) 1px, transparent 1px);
  background-size: 32px 32px;
}
body.has-drawer { overflow: hidden; }
button, input, select, textarea { font: inherit; }
button { color: inherit; }
[hidden] { display: none !important; }
h1, h2, p { margin-top: 0; }

.eyebrow {
  margin-bottom: 5px;
  color: var(--primary-strong);
  font-size: 12px;
  font-weight: 800;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-top: 3px solid var(--primary);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 34px;
  box-shadow: var(--shadow);
}

.login-brand, .sidebar-brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 1px solid oklch(72% 0.1 235);
  border-radius: 8px;
  color: var(--primary-strong);
  background: var(--primary-soft);
  font-weight: 900;
}
.login-brand { margin-bottom: 20px; }
.login-panel h1 { margin-bottom: 9px; color: var(--ink); font-size: 26px; letter-spacing: 0; }
.login-copy { margin-bottom: 25px; color: var(--muted); font-size: 14px; line-height: 1.65; }
.stack-form { display: grid; gap: 16px; }
.form-message { min-height: 21px; margin: 15px 0 0; color: var(--danger); font-size: 13px; line-height: 1.6; }

.admin-shell { min-height: 100vh; }
.admin-sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 30;
  display: flex;
  width: var(--sidebar-width);
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: oklch(99% 0.006 225);
  box-shadow: 10px 0 28px oklch(42% 0.06 240 / 5%);
}
.sidebar-brand { display: flex; align-items: center; gap: 11px; min-height: 78px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.sidebar-brand > span:last-child { display: grid; min-width: 0; gap: 2px; }
.sidebar-brand strong { overflow: hidden; color: var(--ink); font-size: 15px; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-brand small { color: var(--muted); font-size: 12px; }
.sidebar-brand-mark { width: 40px; height: 40px; flex: 0 0 auto; }
.sidebar-nav { display: grid; gap: 4px; padding: 16px 12px; }
.sidebar-footer { display: grid; gap: 4px; margin-top: auto; padding: 12px; border-top: 1px solid var(--line); }
.nav-button, .nav-link {
  display: flex;
  min-height: 44px;
  width: 100%;
  align-items: center;
  gap: 11px;
  border: 1px solid transparent;
  border-radius: 7px;
  color: var(--text);
  background: transparent;
  padding: 0 12px;
  font-size: 14px;
  font-weight: 650;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: color var(--ease), background var(--ease), border-color var(--ease);
}
.nav-button svg, .nav-link svg { width: 18px; height: 18px; flex: 0 0 auto; color: var(--muted); }
.nav-button:hover, .nav-link:hover { border-color: var(--line); background: var(--surface-soft); }
.nav-button.is-active { border-color: oklch(80% 0.08 232); color: var(--primary-strong); background: var(--primary-soft); }
.nav-button.is-active svg { color: var(--primary-strong); }

.admin-main { min-height: 100vh; margin-left: var(--sidebar-width); padding: 0 30px 40px; }
.admin-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  min-height: 78px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--line);
  background: oklch(97% 0.014 225 / 94%);
  backdrop-filter: blur(8px);
}
.admin-header h1 { margin: 0; color: var(--ink); font-size: 23px; letter-spacing: 0; }
.header-status { display: inline-flex; min-height: 34px; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); padding: 0 11px; color: var(--muted); font-size: 12px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 4px oklch(61% 0.14 164 / 14%); }
.page { width: min(1320px, 100%); margin: 24px auto 0; }

.metric-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 16px; }
.metric {
  display: flex;
  min-height: 108px;
  align-items: center;
  gap: 15px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 18px;
  box-shadow: 0 8px 22px oklch(42% 0.06 240 / 5%);
}
.metric-icon { display: grid; width: 42px; height: 42px; flex: 0 0 auto; place-items: center; border: 1px solid oklch(82% 0.075 232); border-radius: 7px; color: var(--primary-strong); background: var(--primary-soft); }
.metric-icon svg { width: 20px; height: 20px; }
.metric > div { display: grid; gap: 4px; }
.metric strong { color: var(--ink); font-size: 25px; line-height: 1; }
.metric span:last-child { color: var(--muted); font-size: 12px; line-height: 1.4; }

.panel { margin-bottom: 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 20px; box-shadow: 0 8px 22px oklch(42% 0.06 240 / 5%); }
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.panel-heading h2, .notice-panel h2 { margin-bottom: 5px; color: var(--ink); font-size: 17px; letter-spacing: 0; }
.panel-heading p, .notice-panel p { max-width: 74ch; margin: 0; color: var(--muted); font-size: 13px; line-height: 1.65; }
.panel-heading-actions { align-items: center; }

.quick-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.quick-action { display: grid; grid-template-columns: 38px minmax(0, 1fr) 18px; align-items: center; gap: 11px; min-height: 74px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); padding: 12px; text-align: left; cursor: pointer; transition: border-color var(--ease), background var(--ease), transform var(--ease); }
.quick-action:hover { border-color: var(--line-strong); background: var(--surface-soft); transform: translateY(-1px); }
.quick-action > svg:first-child { width: 38px; height: 38px; border-radius: 7px; color: var(--primary-strong); background: var(--primary-soft); padding: 9px; }
.quick-action > svg:last-child { width: 17px; color: var(--muted); }
.quick-action > span { display: grid; gap: 4px; min-width: 0; }
.quick-action strong { color: var(--ink); font-size: 14px; }
.quick-action small { overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.notice-panel { display: flex; align-items: flex-start; gap: 13px; box-shadow: none; background: oklch(97% 0.025 230); }
.notice-icon { display: grid; width: 32px; height: 32px; flex: 0 0 auto; place-items: center; border-radius: 7px; color: var(--primary-strong); background: var(--primary-soft); }
.notice-icon svg { width: 17px; height: 17px; }
.danger-note .notice-icon { color: var(--warning); background: oklch(96% 0.045 78); }

.button, .icon-button {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border-radius: 7px;
  padding: 0 14px;
  font-weight: 750;
  cursor: pointer;
  transition: border-color var(--ease), background var(--ease), color var(--ease), transform var(--ease), opacity var(--ease);
}
.button { border: 1px solid var(--line-strong); font-size: 13px; }
.button svg { width: 16px; height: 16px; }
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:focus-visible, .icon-button:focus-visible, .nav-button:focus-visible, .nav-link:focus-visible, .quick-action:focus-visible { outline: 3px solid oklch(68% 0.14 235 / 24%); outline-offset: 2px; }
.button:disabled { cursor: wait; opacity: .62; }
.button-primary { border-color: var(--primary-strong); color: white; background: var(--primary-strong); }
.button-primary:hover:not(:disabled) { background: var(--primary); }
.button-secondary { color: var(--text); background: var(--surface); }
.button-secondary:hover:not(:disabled) { border-color: var(--primary); color: var(--primary-strong); background: var(--primary-soft); }
.button-danger { border-color: oklch(82% 0.1 28); color: var(--danger); background: oklch(97% 0.03 28); }
.button-danger:hover:not(:disabled) { color: white; background: var(--danger); }
.button-block { width: 100%; }
.icon-button { width: 40px; min-width: 40px; border: 1px solid var(--line); background: var(--surface); padding: 0; }
.icon-button:hover { border-color: var(--primary); color: var(--primary-strong); background: var(--primary-soft); }
.icon-button svg { width: 18px; height: 18px; }
.small-icon-button { width: 34px; min-width: 34px; min-height: 34px; }

.field { display: grid; gap: 7px; min-width: 0; color: var(--text); font-size: 13px; font-weight: 700; }
.field small { color: var(--muted); font-size: 11px; font-weight: 500; line-height: 1.5; }
input, select, textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  color: var(--ink);
  background: var(--surface);
  padding: 10px 11px;
  outline: none;
  transition: border-color var(--ease), box-shadow var(--ease), background var(--ease);
}
input, select { min-height: 42px; }
textarea { resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px oklch(68% 0.14 235 / 14%); }
input::placeholder, textarea::placeholder { color: oklch(67% 0.025 242); }
.form-grid { display: grid; gap: 15px; }
.endpoint-grid, .two-column-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.three-column-grid, .compact-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field-wide { grid-column: span 3; }
.setting-group { display: grid; align-content: start; gap: 12px; min-width: 0; margin: 0; border: 1px solid var(--line); border-radius: 7px; padding: 14px; }
.setting-group legend { padding: 0 6px; color: var(--ink); font-size: 13px; font-weight: 800; }
.settings-form > .panel:last-child { margin-bottom: 0; }

.device-create-form {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto;
  align-items: end;
  gap: 12px;
}

.device-code-result {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(220px, auto) 40px;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  border: 1px solid oklch(76% 0.11 164);
  border-radius: 7px;
  background: oklch(96% 0.026 164);
  padding: 13px;
}

.device-code-result > div { display: grid; gap: 3px; }
.device-code-result strong { color: var(--ink); font-size: 13px; }
.device-code-result span { color: var(--muted); font-size: 11px; line-height: 1.45; }
.device-code-result code,
.device-code-hint {
  color: var(--primary-strong);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
}
.device-code-result code { overflow-wrap: anywhere; }

.switch-list { display: grid; gap: 10px; }
.storage-switches { margin-top: 15px; }
.switch-row { display: flex; min-height: 66px; align-items: center; justify-content: space-between; gap: 18px; border: 1px solid var(--line); border-radius: 7px; padding: 12px 14px; cursor: pointer; }
.switch-row > span { display: grid; gap: 4px; }
.switch-row strong { color: var(--ink); font-size: 13px; }
.switch-row small { color: var(--muted); font-size: 12px; font-weight: 500; line-height: 1.45; }
input[role="switch"] { position: relative; width: 42px; min-width: 42px; height: 24px; min-height: 24px; appearance: none; border: 0; border-radius: 999px; background: oklch(78% 0.025 240); padding: 0; cursor: pointer; }
input[role="switch"]::after { position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: white; box-shadow: 0 2px 6px oklch(30% 0.03 245 / 20%); content: ""; transition: transform var(--ease); }
input[role="switch"]:checked { background: var(--success); }
input[role="switch"]:checked::after { transform: translateX(18px); }
.compact-switch { min-height: 60px; }

.table-wrap { position: relative; overflow-x: auto; border: 1px solid var(--line); border-radius: 7px; }
table { width: 100%; border-collapse: collapse; background: var(--surface); font-size: 13px; }
th, td { border-bottom: 1px solid var(--line); padding: 11px 12px; text-align: left; vertical-align: middle; }
th { color: var(--muted); background: var(--surface-soft); font-size: 12px; font-weight: 800; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: oklch(98% 0.012 230); }
.actions-column { width: 218px; }
.material-cell { display: flex; min-width: 170px; align-items: center; gap: 10px; }
.material-thumb { width: 48px; height: 48px; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 7px; object-fit: cover; background: var(--surface-soft); }
.material-cell > span { display: grid; min-width: 0; gap: 3px; }
.material-cell strong { overflow: hidden; max-width: 220px; color: var(--ink); text-overflow: ellipsis; white-space: nowrap; }
.material-cell small, .table-muted { color: var(--muted); font-size: 11px; }
.status-badge { display: inline-flex; min-height: 25px; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 6px; padding: 0 8px; color: var(--muted); background: var(--surface-soft); font-size: 11px; font-weight: 750; white-space: nowrap; }
.status-badge::before { width: 6px; height: 6px; border-radius: 50%; background: currentColor; content: ""; }
.status-badge.is-enabled { border-color: oklch(82% 0.08 164); color: oklch(50% 0.13 164); background: oklch(96% 0.03 164); }
.table-actions { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.table-actions .button { min-height: 34px; padding: 0 10px; }
.audio-button.is-playing { border-color: var(--primary); color: white; background: var(--primary-strong); }
.sort-controls { display: inline-flex; gap: 4px; }
.empty-state { display: grid; min-height: 260px; place-items: center; align-content: center; gap: 7px; color: var(--muted); text-align: center; }
.empty-state svg { width: 28px; height: 28px; color: var(--primary); }
.empty-state strong { color: var(--ink); font-size: 14px; }
.empty-state span { font-size: 12px; }

.key-list { display: grid; gap: 10px; }
.key-row { display: grid; grid-template-columns: minmax(220px, 1fr) 90px minmax(260px, 1.25fr); align-items: center; gap: 14px; border-bottom: 1px solid var(--line); padding: 5px 0 15px; }
.key-row:last-child { border-bottom: 0; padding-bottom: 0; }
.key-row > span:first-child { display: grid; gap: 4px; }
.key-row strong { color: var(--ink); font-size: 13px; }
.key-row small { color: var(--muted); font-size: 11px; font-weight: 500; }
.key-state { justify-self: start; border-radius: 6px; background: var(--surface-soft); padding: 5px 8px; color: var(--muted); font-size: 11px; font-weight: 750; }
.key-state.is-configured { color: oklch(49% 0.13 164); background: oklch(95% 0.035 164); }

dialog { max-width: none; border: 0; background: transparent; padding: 0; }
dialog::backdrop { background: oklch(18% 0.035 250 / 43%); }
.editor-dialog { width: min(560px, calc(100vw - 28px)); }
.confirm-dialog { width: min(390px, calc(100vw - 28px)); }
.dialog-shell { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: 0 26px 70px oklch(25% 0.06 245 / 24%); }
.dialog-header { display: flex; align-items: center; justify-content: space-between; gap: 15px; border-bottom: 1px solid var(--line); padding: 17px 18px; }
.dialog-header h2 { margin: 0; color: var(--ink); font-size: 18px; }
.dialog-body { display: grid; gap: 15px; max-height: min(68vh, 680px); overflow-y: auto; padding: 18px; }
.dialog-footer { display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); padding: 13px 18px; }
.image-preview { display: grid; min-height: 180px; overflow: hidden; place-items: center; border: 1px dashed var(--line-strong); border-radius: 7px; color: var(--muted); background: var(--surface-soft); font-size: 12px; text-align: center; }
.image-preview img { display: block; width: 100%; height: 240px; object-fit: contain; background: white; }
.dialog-audio { width: 100%; height: 40px; }
.confirm-dialog .dialog-shell { padding-top: 24px; text-align: center; }
.confirm-dialog h2 { margin-bottom: 8px; color: var(--ink); font-size: 18px; }
.confirm-dialog p { margin: 0 22px 23px; color: var(--muted); font-size: 13px; line-height: 1.6; }
.confirm-icon { display: grid; width: 42px; height: 42px; margin: 0 auto 13px; place-items: center; border-radius: 7px; color: var(--danger); background: oklch(96% 0.04 28); }
.confirm-icon svg { width: 20px; height: 20px; }

.toast-region { position: fixed; right: 20px; bottom: 20px; z-index: 100; display: grid; width: min(360px, calc(100vw - 28px)); gap: 8px; pointer-events: none; }
.toast { display: flex; align-items: flex-start; gap: 9px; border: 1px solid var(--line); border-left: 4px solid var(--primary); border-radius: 7px; background: var(--surface); padding: 12px 13px; box-shadow: var(--shadow); color: var(--text); font-size: 13px; line-height: 1.5; animation: toast-in 180ms ease-out; }
.toast.is-success { border-left-color: var(--success); }
.toast.is-error { border-left-color: var(--danger); }
.toast svg { width: 17px; height: 17px; flex: 0 0 auto; margin-top: 1px; }
.loading-cell { height: 150px; color: var(--muted); text-align: center; }
.is-loading { cursor: wait !important; }
.mobile-menu-button { display: none; position: fixed; top: 12px; left: 12px; z-index: 45; box-shadow: var(--shadow); }
.sidebar-backdrop { position: fixed; inset: 0; z-index: 35; background: oklch(18% 0.035 250 / 40%); }

@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 1080px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .compact-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .three-column-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field-wide { grid-column: span 2; }
  .key-row { grid-template-columns: minmax(190px, 1fr) 86px minmax(220px, 1fr); }
}

@media (max-width: 820px) {
  .mobile-menu-button { display: inline-flex; }
  .admin-sidebar { z-index: 40; transform: translateX(-102%); transition: transform var(--ease); }
  .admin-sidebar.is-open { transform: translateX(0); }
  .admin-main { margin-left: 0; padding: 0 16px 30px; }
  .admin-header { min-height: 66px; padding-left: 52px; }
  .admin-header .eyebrow { display: none; }
  .admin-header h1 { font-size: 19px; }
  .page { margin-top: 16px; }
  .endpoint-grid, .two-column-grid { grid-template-columns: 1fr; }
  .quick-grid { grid-template-columns: 1fr; }
  .key-row { grid-template-columns: minmax(0, 1fr) auto; }
  .key-row input { grid-column: 1 / -1; }
  .device-code-result { grid-template-columns: minmax(0, 1fr) 40px; }
  .device-code-result > div { grid-column: 1 / -1; }
}

@media (max-width: 580px) {
  .login-shell { padding: 14px; }
  .login-panel { padding: 25px 20px; }
  .admin-main { padding-inline: 10px; }
  .admin-header { margin-inline: -10px; padding-right: 10px; padding-left: 62px; }
  .header-status span:last-child { display: none; }
  .header-status { width: 34px; justify-content: center; padding: 0; }
  .metric-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .metric { min-height: 96px; display: grid; gap: 9px; padding: 13px; }
  .metric-icon { width: 34px; height: 34px; }
  .metric strong { font-size: 21px; }
  .panel { padding: 14px; }
  .panel-heading { display: grid; gap: 12px; }
  .panel-heading-actions .button { width: 100%; }
  .device-create-form { grid-template-columns: 1fr; }
  .device-create-form .button { width: 100%; }
  .three-column-grid, .compact-grid { grid-template-columns: 1fr; }
  .field-wide { grid-column: auto; }
  .switch-row { align-items: flex-start; }
  .table-wrap { border: 0; overflow: visible; }
  table, thead, tbody, tr, th, td { display: block; }
  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
  tbody { display: grid; gap: 10px; }
  tbody tr { display: grid; grid-template-columns: 1fr auto; gap: 9px 12px; border: 1px solid var(--line); border-radius: 7px; padding: 12px; }
  tbody tr:hover { background: var(--surface); }
  td { border: 0; padding: 0; }
  td[data-label]::before { display: block; margin-bottom: 4px; color: var(--muted); content: attr(data-label); font-size: 10px; font-weight: 750; }
  td:first-child, td:nth-child(2), td:last-child { grid-column: 1 / -1; }
  .material-cell strong { max-width: 260px; }
  .actions-column { width: auto; }
  .table-actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .table-actions .button { padding: 0 8px; }
  .sort-controls { justify-self: start; }
  .dialog-footer .button { flex: 1; }
  .toast-region { right: 14px; bottom: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
