:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #16181d;
  --muted: #5d6470;
  --border: #dde2e8;
  --accent: #2f6fde;
  --accent-hover: #2459b8;
  --accent-text: #ffffff;
  --accent-soft: #e4edfc;
  --danger: #c62f2f;
  --danger-soft: #fbe7e7;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --ok: #1a7f44;
  --ok-soft: #e0f4e8;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 12px;
  --gutter: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1a1d23;
    --surface-2: #242830;
    --text: #e9ebef;
    --muted: #9aa2ae;
    --border: #2f343d;
    --accent: #6a9cf5;
    --accent-hover: #8bb3f8;
    --accent-text: #0d1424;
    --accent-soft: #1f2c44;
    --danger: #f07171;
    --danger-soft: #3a2023;
    --warn: #f0b45a;
    --warn-soft: #3a2e1a;
    --ok: #5fd08f;
    --ok-soft: #1a3326;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow-x: hidden;
}

a { color: var(--accent); }

h1, h2, h3 { line-height: 1.25; margin: 0 0 8px; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 8px; }

.container {
  max-width: 760px;
  margin: 0 auto;
  padding: 16px var(--gutter) 48px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 var(--gutter);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 56px;
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.1rem; }
.nav { display: flex; gap: 4px; }
.nav a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
}
.nav a.active { color: var(--accent); background: var(--accent-soft); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
  min-width: 0;
}
.card > :last-child { margin-bottom: 0; }

.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.nowrap { flex-wrap: nowrap; }
.stack > * + * { margin-top: 12px; }
.grow { flex: 1 1 auto; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.center { text-align: center; }
.pad { padding: 24px 0; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.wrap-any { overflow-wrap: anywhere; word-break: break-word; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; }
.back { display: inline-flex; align-items: center; min-height: 44px; text-decoration: none; font-weight: 600; }

button, .btn {
  font: inherit;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  touch-action: manipulation;
}
button:hover, .btn:hover { border-color: var(--muted); }
button:disabled { opacity: .55; cursor: default; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button.danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
button.ghost { background: transparent; border-color: transparent; color: var(--accent); }
button.small { min-height: 44px; padding: 0 12px; font-size: .9rem; }
button.full { width: 100%; }
.btn-group { display: flex; flex-wrap: wrap; gap: 8px; }

label { display: block; font-weight: 600; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.hint { color: var(--muted); font-size: .875rem; font-weight: 400; margin-top: 4px; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="time"], select, textarea {
  font: inherit;
  font-size: 16px;
  font-weight: 400;
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input[type="checkbox"] { width: 22px; height: 22px; margin: 0; flex: none; accent-color: var(--accent); }

.check {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 0;
  font-weight: 400;
  cursor: pointer;
}
.check strong { display: block; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 700;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--accent-soft); color: var(--accent); }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--ok); }

.notice {
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--surface-2);
  margin-bottom: 12px;
}
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.danger { background: var(--danger-soft); color: var(--danger); }
.notice.info { background: var(--accent-soft); }

/* child card */
.child-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.child-head h2 { margin: 0; overflow-wrap: anywhere; }
.big-num { font-size: 1.6rem; font-weight: 700; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 10px 0; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.stat .label { font-size: .8rem; color: var(--muted); }
.stat .value { font-weight: 700; font-size: 1.05rem; overflow-wrap: anywhere; }
.progress { height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.progress.over > span { background: var(--danger); }
.device-line { display: flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--muted); overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.actions button { flex: 1 1 auto; }

/* editor */
.day-grid { display: grid; grid-template-columns: 56px minmax(0, 1fr); gap: 8px 12px; align-items: center; }
.day-name { font-weight: 600; }
.hours-day { border-top: 1px solid var(--border); padding: 12px 0; }
.hours-day:first-of-type { border-top: 0; }
.window-row { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) auto; gap: 8px; align-items: center; margin-top: 8px; }
.rule { border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin-bottom: 12px; }
.two-col { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 12px; }
.sticky-save {
  position: sticky;
  bottom: 0;
  background: var(--bg);
  padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}
.sticky-save button { flex: 1; }

/* devices */
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 12px; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.account-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 8px; align-items: center; padding: 6px 0; }
.account-row .name { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow-wrap: anywhere; }
details.section { border-top: 1px solid var(--border); margin-top: 12px; }
details.section > summary {
  cursor: pointer;
  min-height: 44px;
  display: flex;
  align-items: center;
  font-weight: 600;
  list-style: none;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::before { content: "▸"; margin-right: 8px; color: var(--muted); }
details.section[open] > summary::before { content: "▾"; }

.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-align: center;
  padding: 12px;
  background: var(--surface-2);
  border-radius: 10px;
  user-select: all;
}
pre.cmd {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .85rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-all;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 12px;
  margin: 8px 0;
  user-select: all;
}
ol.steps { padding-left: 20px; margin: 8px 0; }
ol.steps li { margin-bottom: 6px; }

/* reports */
.chart { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; align-items: end; height: 180px; margin-top: 8px; }
.bar-col { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.bar-val { font-size: .72rem; color: var(--muted); margin-bottom: 4px; white-space: nowrap; }
.bar { width: 100%; max-width: 44px; background: var(--accent); border-radius: 6px 6px 2px 2px; min-height: 2px; }
.bar.over { background: var(--danger); }
.bar-labels { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; margin-top: 6px; }
.bar-labels span { text-align: center; font-size: .8rem; color: var(--muted); }
.bar-labels span.today { color: var(--text); font-weight: 700; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.list .name { min-width: 0; overflow-wrap: anywhere; }
.list .val { white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; }
.tabs { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.tabs button { min-height: 44px; border: 0; background: transparent; color: var(--muted); padding: 0 12px; }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.event { padding: 10px 0; border-bottom: 1px solid var(--border); }
.event:last-child { border-bottom: 0; }
.event .meta { font-size: .85rem; color: var(--muted); overflow-wrap: anywhere; }
.event .detail { font-size: .9rem; overflow-wrap: anywhere; }

/* auth */
.auth { max-width: 420px; margin: 24px auto; }

/* dialog */
dialog {
  border: 0;
  padding: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  width: min(520px, calc(100vw - 2 * var(--gutter)));
  max-height: calc(100dvh - 32px);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog-body { padding: 20px 16px 8px; overflow-y: auto; max-height: calc(100dvh - 120px); }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px 16px; flex-wrap: wrap; }
.dialog-actions button { flex: 1 1 auto; }

.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(480px, calc(100vw - 2 * var(--gutter)));
  pointer-events: none;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 12px 14px;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  overflow-wrap: anywhere;
}
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 520px) {
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .stat { padding: 6px 8px; }
  .two-col { grid-template-columns: minmax(0, 1fr); }
  .account-row { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .nav a { padding: 0 10px; }
  .code { font-size: 1.45rem; }
}

.auth .google { margin: 8px 0 4px; font-weight: 600; }
.auth .or { display: flex; align-items: center; gap: 12px; margin: 16px 0; color: var(--muted); font-size: 0.875rem; }
.auth .or::before, .auth .or::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

#admin-badge { margin-left: 8px; }
.auth .qr { display: block; width: 200px; height: 200px; margin: 12px auto; padding: 8px; background: #fff; border-radius: 8px; }

.topbar-actions { display: flex; gap: 4px; margin-left: auto; }
.topbar-actions button { white-space: nowrap; }
@media (max-width: 600px) {
  .topbar-inner { flex-wrap: wrap; }
  .topbar-actions { order: 2; }
  .nav { order: 3; width: 100%; }
}
