:root {
  color-scheme: light;
  --bg: #f3f0e8;
  --bg-2: #e7efe4;
  --surface: #fffdf8;
  --ink: #1d1c19;
  --muted: #6f6b62;
  --line: rgba(29, 28, 25, 0.1);
  --amber: #e39b2b;
  --amber-deep: #9a5b0b;
  --on: #f3c56a;
  --on-ink: #2a2112;
  --good: #2d6b45;
  --water: #315f74;
  --danger: #a33b2b;
  --shadow: 0 12px 30px rgba(70, 50, 20, 0.06);
  --radius: 16px;
  --pad: 16px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #10120f;
  --bg-2: #1a241c;
  --surface: #1b1e19;
  --ink: #f4f1ea;
  --muted: #b1ab9f;
  --line: rgba(244, 241, 234, 0.1);
  --amber: #f0b429;
  --amber-deep: #f0b429;
  --on: #f0b429;
  --on-ink: #241c0e;
  --good: #b7d7a8;
  --water: #b7d4e2;
  --danger: #f0a090;
  --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.4;
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 0% -10%, var(--bg-2), transparent 55%),
    var(--bg);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
img, svg { display: block; }
:focus-visible { outline: 3px solid var(--amber-deep); outline-offset: 2px; }
h1, h2, h3, p { margin: 0; }

.glyph svg { width: 20px; height: 20px; }
.logo svg { width: 28px; height: 28px; }

.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.gate-card {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 28px;
  box-shadow: var(--shadow);
  padding: 28px 22px 22px;
}
.gate h1 {
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: 2.4rem;
  letter-spacing: -0.04em;
  font-weight: 600;
  margin-top: 12px;
}
.lede { color: var(--muted); margin: 8px 0 20px; }
.form { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-size: 0.92rem; }
input[type="url"], input[type="password"], input[type="text"], input[type="search"], input[type="time"], select {
  width: 100%;
  min-height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: transparent;
  padding: 0 14px;
}
.check { display: flex; align-items: center; gap: 10px; }
.check input { width: 20px; height: 20px; }
.form-error { color: var(--danger); min-height: 1.2em; }
.form-ok { color: var(--good); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
button.primary, button.secondary, button.ghost, .seg button, .choices button, .stepper button, .lock-btn, .tabbar button, .side-views button, .rooms button, .star {
  border: 0;
  background: transparent;
}
button.primary, button.secondary {
  min-height: 48px;
  padding: 0 16px;
  border-radius: 14px;
}
button.primary { background: var(--ink); color: var(--bg); }
:root[data-theme="dark"] button.primary { background: var(--amber); color: var(--on-ink); }
button.secondary { border: 1px solid var(--line); }
button.ghost { min-height: 44px; padding: 0 8px; color: var(--muted); text-decoration: underline; }
.help { color: var(--muted); font-size: 0.92rem; }

#frame { min-height: 100dvh; }
.top, .rooms, main { padding-left: var(--pad); padding-right: var(--pad); }
.top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: calc(8px + env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand > div { display: grid; line-height: 1.15; }
.brand strong {
  font-size: 1.25rem;
  font-weight: 650;
  letter-spacing: -0.03em;
}
.brand span, .side-note { color: var(--muted); font-size: 0.85rem; }
.search { flex: 1; position: relative; }
.search .glyph { position: absolute; left: 12px; top: 13px; color: var(--muted); }
.search input { padding-left: 40px; background: var(--surface); }
.icon-btn[hidden] { display: none; }
.icon-btn.is-active {
  background: var(--ink);
  color: var(--bg);
  border-color: transparent;
}
:root[data-theme="dark"] .icon-btn.is-active {
  background: var(--amber);
  color: var(--on-ink);
}
.icon-btn, .star {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface);
}
.star { border: 0; background: transparent; color: #fff; font-size: 1.2rem; }
.star[aria-pressed="true"] { color: #e10600; }

.banner {
  margin: 12px var(--pad) 0;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
}
.banner.is-error { color: var(--danger); }
.rooms {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-top: 12px;
  padding-bottom: 8px;
  position: sticky;
  top: 0;
  z-index: 4;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  scrollbar-width: none;
}
.rooms::-webkit-scrollbar { display: none; }
.rooms button, .side-rooms button, .side-views button {
  min-height: 40px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  white-space: nowrap;
}
.rooms button.is-active, .side-rooms button.is-active, .side-views button.is-active, .choices button.is-active {
  background: var(--ink);
  color: var(--bg);
  border-color: transparent;
}
:root[data-theme="dark"] .rooms button.is-active,
:root[data-theme="dark"] .side-rooms button.is-active,
:root[data-theme="dark"] .side-views button.is-active,
:root[data-theme="dark"] .choices button.is-active {
  background: var(--amber);
  color: var(--on-ink);
}

main { padding-bottom: 28px; }
.room-block { margin-top: 16px; }
.room-block h2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 1.15rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.room-title { flex: 1; min-width: 0; }
.room-aside { color: var(--muted); font-size: 0.9rem; font-weight: 550; font-variant-numeric: tabular-nums; }
.grip {
  width: 28px;
  min-width: 28px;
  height: 28px;
  min-height: 28px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: grab;
  touch-action: none;
  letter-spacing: -0.18em;
  font-size: 0.95rem;
  line-height: 1;
}
.grip:active, .is-dragging .grip { cursor: grabbing; }
.is-dragging { opacity: 0.45; }
.grid.tiles > .row { grid-column: 1 / -1; }
.section { margin-top: 14px; }
.section h3 {
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stack { display: grid; gap: 8px; }

.tile, .row, .sensor, .scenario, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.tile { padding: 8px 6px 8px 8px; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
.tile-line, .row-top { display: flex; align-items: flex-start; gap: 8px; min-width: 0; }
.tile-line { justify-content: space-between; }
.tile-hit {
  border: 0;
  background: transparent;
  text-align: left;
  padding: 0;
  display: grid;
  gap: 2px;
  min-width: 0;
  width: 100%;
}
.tile-copy, .row-top > div { min-width: 0; flex: 1; display: grid; }
.tile-name, .sensor-name, .row h3, .scenario strong {
  font-size: 0.92rem;
  font-weight: 650;
  line-height: 1.25;
  white-space: normal;
  overflow: visible;
}
.tile-state:empty { display: none; }
.tile-state, .tile-extra, .sensor-extra, .row p, .scenario small {
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-hit {
  border: 0;
  background: transparent;
  padding: 4px;
  margin: -4px;
  min-width: 44px;
  min-height: 44px;
  display: grid;
  place-items: center;
  touch-action: manipulation;
}
.icon-bubble {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: none;
  background: color-mix(in srgb, var(--ink) 7%, transparent);
  color: var(--amber-deep);
  transition: background 120ms ease, color 120ms ease;
}
.tile.is-on .icon-bubble { background: #f6d98a; color: #9a5b0b; }
:root[data-theme="dark"] .tile.is-on .icon-bubble { background: #f0b429; color: #241c0e; }
.tile.is-pulse .icon-bubble {
  background: color-mix(in srgb, #f0b429 32%, #fff);
  color: #f0b429;
}
:root[data-theme="dark"] .tile.is-pulse .icon-bubble {
  background: color-mix(in srgb, #f0b429 28%, transparent);
  color: #ffe082;
}
.tile.is-pulse .icon-bubble svg { filter: drop-shadow(0 0 2px currentColor); }
.sensor.is-open .icon-bubble { background: color-mix(in srgb, var(--amber) 35%, var(--surface)); }
.tile.is-alert, .sensor.is-alert, .row.is-alert { border-color: var(--danger); }
.sensor.is-open, .row.is-open { border-color: var(--amber-deep); }
.badge, .battery {
  font-size: 0.75rem;
  color: var(--muted);
}
.card-tools { margin-left: auto; display: flex; align-items: center; flex: none; }
.card-tools .star, .card-tools .more {
  width: 26px;
  height: 26px;
  min-width: 26px;
  min-height: 26px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
}
.card-tools .star { color: #fff; }
.card-tools .star[aria-pressed="true"] { color: #e10600; }
:root:not([data-theme="dark"]) .card-tools .star:not([aria-pressed="true"]) {
  -webkit-text-stroke: 1px #1d1c19;
  paint-order: stroke fill;
}
.more { font-size: 1.25rem; line-height: 1; border-radius: 10px; }
.device-menu { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 8;
  min-width: 180px;
  padding: 6px;
  display: grid;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.menu button {
  min-height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  padding: 0 10px;
}
.menu button.is-danger { color: var(--danger); }
.tile.is-on .more { color: inherit; }
.tile:has(.menu:not([hidden])),
.row:has(.menu:not([hidden])),
.sensor:has(.menu:not([hidden])) { position: relative; z-index: 5; }
.concealed { display: grid; gap: 8px; }
.concealed h3 { font-size: 0.95rem; }
.concealed-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.concealed-row small { display: block; color: var(--muted); }
.battery.is-low, .badge { color: var(--danger); }

.range input, input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 32px;
  background: transparent;
  margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.12);
}
:root[data-theme="dark"] input[type="range"]::-webkit-slider-runnable-track { background: rgba(255, 255, 255, 0.16); }
.tile.is-on input[type="range"]::-webkit-slider-runnable-track { background: rgba(36, 28, 14, 0.2); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -10px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.08);
}
input[type="range"]::-moz-range-track { height: 6px; border: 0; border-radius: 99px; background: rgba(0, 0, 0, 0.12); }
input[type="range"]::-moz-range-thumb { width: 26px; height: 26px; border: 0; border-radius: 50%; background: #fff; }

.row { padding: 10px; display: grid; gap: 8px; }
.temp {
  font-size: 1.35rem;
  font-weight: 650;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
}
.climate-main { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.climate .mode-select { min-height: 36px; background: var(--bg); }
.climate .stepper button { min-height: 36px; width: 36px; padding: 0; }
.climate .stepper strong { min-width: 72px; font-size: 1rem; }
.seg, .choices, .stepper, .dialog-actions, .theme-row { display: flex; gap: 8px; }
.choices { flex-wrap: wrap; }
.mode-field select {
  color: var(--ink);
  background: var(--bg);
}
.seg { display: grid; grid-template-columns: repeat(3, 1fr); }
.seg button, .choices button, .stepper button, .lock-btn, .theme-row button {
  min-height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg);
  padding: 0 10px;
}
.stepper strong { min-width: 92px; display: grid; place-items: center; font-size: 1.3rem; }
.lock-btn, .scenario { width: 100%; text-align: left; }
.scenario {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 14px;
  min-height: 76px;
}
.scenario.is-on { border-color: var(--amber-deep); }
.scenarios { display: grid; gap: 10px; }
.schedule-current { color: var(--muted); margin: -4px 0 10px; }
.schedule-times { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.weekdays { margin-top: 2px; }
.day {
  min-height: 42px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  display: inline-grid;
  place-items: center;
}
.day.is-active, .weekdays button.is-active { color: var(--on-ink); background: var(--on); border-color: transparent; }
.period {
  margin-left: auto;
  color: var(--amber-deep);
  font-size: 0.85rem;
  font-weight: 650;
}
button:disabled { opacity: 0.4; cursor: default; }
.panel { padding: 16px; display: grid; gap: 14px; }
.empty { padding: 32px 8px; display: grid; gap: 8px; }
.empty h2 { font-family: Georgia, "Iowan Old Style", Palatino, serif; font-size: 1.8rem; }

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
  z-index: 6;
}
.tabbar button { min-height: 48px; border-radius: 14px; color: var(--muted); font-size: 0.92rem; }
.tabbar button.is-active { color: var(--ink); background: var(--bg); }
#content { padding-bottom: calc(88px + env(safe-area-inset-bottom)); }
aside { display: none; }

.dialog {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(16, 18, 15, 0.46);
  display: grid;
  place-items: center;
  padding: 16px;
}
.dialog-card {
  width: min(420px, 100%);
  background: var(--surface);
  color: var(--ink);
  border-radius: 24px;
  padding: 20px;
  display: grid;
  gap: 12px;
}
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(100px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  max-width: min(420px, calc(100% - 24px));
  background: var(--ink);
  color: var(--bg);
  border-radius: 999px;
  padding: 12px 16px;
}

@media (max-width: 979px) {
  .brand.mobile-only { flex: none; }
  .brand.mobile-only [data-clock] { display: none; }
  .search { flex: 1; min-width: 0; }
}
@media (min-width: 720px) {
  .grid.tiles { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 980px) {
  #frame { display: grid; grid-template-columns: 280px minmax(0, 1fr); }
  aside {
    display: flex;
    flex-direction: column;
    gap: 18px;
    position: sticky;
    top: 0;
    height: 100dvh;
    overflow: auto;
    padding: 22px 16px;
    border-right: 1px solid var(--line);
  }
  .side-views, .side-rooms { display: grid; gap: 6px; }
  .side-views button, .side-rooms button { text-align: left; border-radius: 12px; }
  .mobile-only, .tabbar, #rooms { display: none !important; }
  #content { padding: 8px 12px 40px 8px; max-width: 1180px; }
  .grid.tiles { grid-template-columns: repeat(4, 1fr); }
  .grid.tiles > .row.climate { grid-column: span 2; }
  .grid.tiles > :not(.climate) + .row.climate { grid-column: 1 / span 2; }
  .stack { grid-template-columns: 1fr 1fr; }
  .stack > :not(.climate) { grid-column: 1 / -1; }
  .toast { bottom: 24px; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}
