:root {
  --bg: #f7f4ee;
  --surface: #ffffff;
  --ink: #1d2620;
  --muted: #667068;
  --line: #e4dfd4;
  --accent: #1f7a4d;
  --accent-ink: #ffffff;
  --accent-soft: #e0f1e8;
  --warn: #9a5b00;
  --warn-soft: #fff0d2;
  --danger: #b3261e;
  --danger-soft: #fde7e5;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 30, 20, .06), 0 8px 24px rgba(20, 30, 20, .07);
  --tab-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1411;
    --surface: #171d19;
    --ink: #e9f0ea;
    --muted: #98a59c;
    --line: #26302a;
    --accent: #4cc38a;
    --accent-ink: #06240f;
    --accent-soft: #14301f;
    --warn: #f0b04a;
    --warn-soft: #34290f;
    --danger: #ff8f86;
    --danger-soft: #3a1815;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 ui-rounded, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
}
body.noscroll { overflow: hidden; }
h1, h2 { margin: 0; line-height: 1.2; }
a { color: inherit; text-decoration: none; }
img { display: block; max-width: 100%; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: .88rem; }
.pad { padding: 12px 16px; }
.i { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px;
  border: 1.5px solid var(--line); border-radius: 14px;
  background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }
.btn.grow { flex: 1; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.busy { opacity: .6; pointer-events: none; }
.icon-btn {
  width: 44px; height: 44px; flex: none;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 12px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:active { background: var(--line); }

.field, .search {
  width: 100%; min-height: 48px; padding: 0 14px;
  border: 1.5px solid var(--line); border-radius: 14px;
  background: var(--surface); color: var(--ink);
  font: inherit;
}
.field:focus, .search:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.field.slim { width: 84px; text-align: center; padding: 0 8px; }
.search { margin-bottom: 12px; }
.lbl { display: block; margin-bottom: 16px; font-weight: 600; font-size: .9rem; }
.lbl > .field, .lbl > select, .lbl > .tpl-grid { margin-top: 6px; font-weight: 400; }
.chips { display: flex; gap: 8px; margin-bottom: 12px; }
.chip {
  min-height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1.5px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-size: .9rem; cursor: pointer;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- app shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 1.45rem; letter-spacing: -.01em; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: var(--accent-soft); color: var(--accent);
  font: inherit; font-weight: 700; font-size: .85rem; cursor: pointer;
}
#view { padding: 8px 16px calc(var(--tab-h) + var(--safe-b) + 96px); max-width: 720px; margin: 0 auto; }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: .72rem; font-weight: 600;
}
.tabbar a.on { color: var(--accent); }

.fab {
  position: fixed; right: 16px; bottom: calc(var(--tab-h) + var(--safe-b) + 16px); z-index: 15;
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 54px; padding: 0 22px; border: 0; border-radius: 999px;
  background: var(--accent); color: var(--accent-ink);
  font: inherit; font-weight: 700; box-shadow: var(--shadow); cursor: pointer;
}
.fab:active { transform: scale(.97); }

/* ---------- cards ---------- */
.cards { display: grid; gap: 12px; }
.card {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px;
}
.card-main { display: flex; flex: 1; min-width: 0; gap: 14px; align-items: center; }
.thumb { width: 112px; flex: none; border-radius: 10px; overflow: hidden; background: var(--line); aspect-ratio: 16 / 9; display: grid; place-items: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .schematic { width: 100%; }
.card-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.card-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card.screen { gap: 14px; padding: 16px; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.dot.on { background: #2fb36b; box-shadow: 0 0 0 4px color-mix(in srgb, #2fb36b 25%, transparent); }
.dot.off { background: #b8beb9; }
.badge {
  align-self: flex-start; margin-top: 4px; padding: 2px 10px; border-radius: 999px;
  font-size: .72rem; font-weight: 700; letter-spacing: .02em;
}
.badge.live { background: var(--accent-soft); color: var(--accent); }
.badge.draft { background: var(--warn-soft); color: var(--warn); }

/* ---------- empty / error / skeleton ---------- */
.empty { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; padding: 48px 24px; color: var(--muted); grid-column: 1 / -1; }
.empty strong { color: var(--ink); font-size: 1.05rem; }
.empty .btn { margin-top: 10px; }
.skels { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); width: 100%; }
.skel { border-radius: var(--radius); background: linear-gradient(90deg, var(--line), color-mix(in srgb, var(--line) 50%, var(--surface)), var(--line)); background-size: 200% 100%; animation: sh 1.3s infinite linear; }
.card-skel { height: 84px; }
.tile-skel { aspect-ratio: 1; }
.skels:has(.card-skel) { grid-template-columns: 1fr; }
@keyframes sh { to { background-position: -200% 0; } }

/* ---------- media grid ---------- */
.media-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
@media (min-width: 520px) { .media-grid { grid-template-columns: repeat(4, 1fr); } }
.tile {
  position: relative; aspect-ratio: 1; padding: 0; border: 0; border-radius: 12px; overflow: hidden;
  background: var(--line); cursor: pointer; color: #fff;
}
.tile img { width: 100%; height: 100%; object-fit: cover; }
.tile-name {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 6px 5px;
  font-size: .7rem; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  background: linear-gradient(transparent, rgba(0, 0, 0, .6));
}
.tile.noimg { color: var(--ink); display: grid; place-items: center; }
.tile.noimg .tile-name { background: none; color: var(--muted); }
.tile-type { position: absolute; top: 6px; left: 6px; padding: 1px 8px; border-radius: 999px; background: rgba(0, 0, 0, .6); font-size: .65rem; text-transform: uppercase; letter-spacing: .04em; }
.tile-check {
  position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border-radius: 50%;
  display: none; place-items: center; background: var(--accent); color: var(--accent-ink);
}
.tile-check .i { width: 16px; height: 16px; stroke-width: 3; }
.tile.selected { outline: 3px solid var(--accent); outline-offset: -3px; }
.tile.selected .tile-check { display: grid; }

/* ---------- editor ---------- */
.back { display: inline-flex; align-items: center; gap: 2px; min-height: 44px; color: var(--muted); font-weight: 600; margin-left: -6px; }
.ed-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.ed-head .badge { margin: 0; }
.schematic { position: relative; width: 100%; background: var(--line); border-radius: 8px; overflow: hidden; }
.ed-schematic { margin-bottom: 16px; border-radius: 12px; box-shadow: var(--shadow); }
.zone {
  position: absolute; display: grid; place-items: center;
  border: 2px solid var(--bg); background: color-mix(in srgb, var(--accent) 22%, var(--surface));
  color: var(--accent); font-weight: 700; font-size: .9rem; overflow: hidden;
}
.thumb .zone, .tpl .zone { font-size: .6rem; border-width: 1px; }
.banner { background: var(--warn-soft); color: var(--ink); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; display: grid; gap: 10px; }
.banner p { margin: 0; }
.zone-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.zone-card > .btn { border-radius: 0; border-width: 1px 0 0; }
.zone-head { display: flex; justify-content: space-between; align-items: baseline; padding: 14px 16px 6px; }
.widgets { list-style: none; margin: 0; padding: 0; }
.widget { display: flex; align-items: center; gap: 4px; padding: 8px 8px 8px 12px; border-top: 1px solid var(--line); }
.w-thumb { width: 56px; height: 56px; flex: none; margin-right: 8px; border-radius: 10px; overflow: hidden; background: var(--line); display: grid; place-items: center; color: var(--muted); font-size: .7rem; text-transform: uppercase; font-weight: 700; }
.w-thumb img { width: 100%; height: 100%; object-fit: cover; }
.w-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.w-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .95rem; }
.actionbar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-b)); z-index: 18;
  display: flex; gap: 8px; align-items: center; padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}

/* ---------- sheets ---------- */
.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .45); opacity: 0; transition: opacity .2s; }
.backdrop.show { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-width: 720px; margin: 0 auto; max-height: 86dvh;
  display: flex; flex-direction: column;
  background: var(--bg); border-radius: 24px 24px 0 0; box-shadow: 0 -10px 40px rgba(0, 0, 0, .25);
  transform: translateY(102%); transition: transform .22s cubic-bezier(.2, .8, .2, 1);
}
.sheet.show { transform: none; }
.sheet.tall { height: 86dvh; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 8px 6px 20px; }
.sheet-head h2 { font-size: 1.1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-body { padding: 8px 20px 20px; overflow-y: auto; overscroll-behavior: contain; flex: 1; }
.sheet-foot { display: flex; gap: 10px; align-items: center; justify-content: flex-end; padding: 12px 16px calc(12px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg); }
.sheet-foot .btn.primary { flex: 1; }
.sheet-msg { margin: 4px 0 8px; color: var(--muted); }
.dur { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: .85rem; }
.menu { display: grid; gap: 4px; }
.menu-item { display: flex; align-items: center; min-height: 52px; padding: 0 14px; border: 0; border-radius: 12px; background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; text-align: left; cursor: pointer; }
.menu-item.danger { color: var(--danger); }

.tpl-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tpl { display: grid; gap: 8px; padding: 10px; border: 2px solid var(--line); border-radius: 14px; background: var(--surface); color: var(--ink); font: inherit; font-size: .85rem; font-weight: 600; cursor: pointer; }
.tpl.on { border-color: var(--accent); background: var(--accent-soft); }

.detail-img { border-radius: 14px; overflow: hidden; background: var(--line); margin-bottom: 14px; display: grid; place-items: center; min-height: 120px; }
.detail-img img { max-height: 40dvh; object-fit: contain; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0 0 18px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; text-align: right; }

.up-row { display: grid; gap: 6px; margin-bottom: 14px; }
.up-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.up-row.ok .muted { color: var(--accent); }
.up-row.err .muted { color: var(--danger); }
.up-row.err .bar i { background: var(--danger); width: 100%; }

/* ---------- toasts ---------- */
#toasts { position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-b) + 84px); z-index: 60; display: grid; gap: 8px; justify-items: center; pointer-events: none; padding: 0 16px; }
.toast { max-width: 440px; padding: 12px 18px; border-radius: 14px; background: var(--ink); color: var(--bg); font-weight: 600; font-size: .92rem; box-shadow: var(--shadow); animation: up .2s ease-out; }
.toast.err { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transition: opacity .4s; }
@keyframes up { from { transform: translateY(10px); opacity: 0; } }

/* ---------- welcome (signed out) ---------- */
body.welcome { display: grid; place-items: center; padding: 24px; background: linear-gradient(160deg, var(--accent-soft), var(--bg) 60%); }
.welcome-card { width: 100%; max-width: 380px; text-align: center; display: grid; gap: 14px; justify-items: center; }
.welcome-card h1 { font-size: 2rem; letter-spacing: -.02em; }
.welcome-card p { margin: 0; color: var(--muted); }
.welcome-card .logo { border-radius: 18px; box-shadow: var(--shadow); }
.welcome-card .fine { font-size: .82rem; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
