/*
 * Tesla-style chrome (flat dark tiles, Tesla blue for active/moving, red only for problems)
 * around illustrations of the house itself: the brick garage façade with its white
 * raised-panel doors, and the arched wrought-iron double gate (drawn by art.js).
 *
 * Designed for the smallest target: Model 3 browser in split view, 773×601 CSS px.
 * Larger screens (Cybertruck) get the same composition, scaled up.
 * Theme: Light / Auto / Dark control in the header (app.js sets data-theme on <html>).
 */

@font-face {
  font-family: "Inter";
  src: url("fonts/inter-latin-wght.woff2") format("woff2");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}


:root {
  --bg: #121314;
  --tile: #1f2022;
  --control: #2d2e31;
  --control-down: #393a3e;
  --line: #4a4b50;
  --text: #ffffff;
  --text-2: #9d9ea2;
  --blue: #3e6ae1;          /* Tesla blue */
  --blue-text: #6b8dff;     /* blue that reads on dark tiles */
  --red: #e82127;
  --door-travel: 17s;       /* real door travel time */
  --gate-travel: 15s;       /* real gate travel time */

  /* Monochrome artwork (art.js) */
  --art-line: #e6e7e9;      /* gate iron, fence, lanterns */
  --masonry: #26272b;
  --masonry-line: #36373c;
  --cap: #44464b;
  --lit: #f5f5f3;           /* lit windows, lantern glass, bay glow */
  --wall-bg: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='26' height='10'%3E%3Crect width='26' height='10' fill='%23232427'/%3E%3Cpath d='M0 .5H26M0 5.5H26M13 .5V5.5M6.5 5.5V10.5M19.5 5.5V10.5' stroke='%23313236' stroke-width='1'/%3E%3C/svg%3E");
  --roof: #17181a;
  --fascia: #8e9096;
  --apron: #393a3e;
  --trim: #4a4c52;
  --slab: #3a3c41;
  --panel-line: #6e7177;
  --panel-fill: #404247;
  --joint: #2b2c30;
  --glass: #0c0c0e;
  --interior: #0c0c0e;
  --floor: #18191c;
  --seal: #6e7177;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #eeeff0;
  --tile: #ffffff;
  --control: #ebecee;
  --control-down: #dcdde0;
  --line: #a9abaf;
  --text: #171a20;
  --text-2: #6b6e74;
  --blue-text: #3e6ae1;
  --art-line: #2a2c30;
  --masonry: #dcdde0;
  --masonry-line: #c6c8cb;
  --cap: #c9cbce;
  --lit: #ffe6a6;
  --wall-bg: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='26' height='10'%3E%3Crect width='26' height='10' fill='%23dcdde0'/%3E%3Cpath d='M0 .5H26M0 5.5H26M13 .5V5.5M6.5 5.5V10.5M19.5 5.5V10.5' stroke='%23c8cacd' stroke-width='1'/%3E%3C/svg%3E");
  --roof: #5d6066;
  --fascia: #ffffff;
  --apron: #c4c6c9;
  --trim: #ffffff;
  --slab: #f4f5f6;
  --panel-line: #bfc1c5;
  --panel-fill: #f9f9fa;
  --joint: #d3d5d8;
  --glass: #2a2c30;
  --interior: #2a2c30;
  --floor: #34363b;
  --seal: #a9abaf;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }

body {
  min-height: 100vh;
  padding: 0 12px 8px;
  background: var(--bg);
  color: var(--text);
  font: 500 16px/1.25 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: min(1100px, 150vh);
  margin: 0 auto;
}

/* ---------- Header ---------- */

.topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 36px;
  padding: 0 4px;
}
.header-right { display: flex; align-items: center; gap: 12px; }

/* Appearance: Light / Auto / Dark segmented control */
.theme-control {
  position: relative;
  display: flex;
  padding: 2px;
  border-radius: 999px;
  background: var(--control);
}
.theme-control button {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 34px;
  height: 30px;
  margin: -7px 0;            /* 44 px tall touch target, 30 px visual */
  padding: 7px 0;
  box-sizing: content-box;
  border: 0;
  background: none;
  color: var(--text-2);
  cursor: pointer;
  touch-action: manipulation;
  transition: color 0.2s;
}
.theme-control svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.theme-control svg .fill { fill: currentColor; stroke: none; }
.theme-control button[aria-checked="true"] { color: var(--bg); }
.theme-control button:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; border-radius: 999px; }
.theme-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 34px;
  height: 30px;
  border-radius: 999px;
  background: var(--text);
  transition: transform 0.2s ease;
}
.theme-control[data-mode="auto"] .theme-thumb { transform: translateX(34px); }
.theme-control[data-mode="dark"] .theme-thumb { transform: translateX(68px); }

.conn { display: flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 0.9rem; }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-2); }
.conn[data-conn="live"] .conn-dot { background: var(--blue); }
.conn[data-conn="connecting"] .conn-dot,
.conn[data-conn="reconnecting"] .conn-dot { animation: pulse 1.1s ease-in-out infinite; }
.conn[data-conn="offline"] { color: var(--red); }
.conn[data-conn="offline"] .conn-dot { background: var(--red); }

/* ---------- Tiles & status text ---------- */

.tile {
  padding: 12px;
  border-radius: 14px;
  background: var(--tile);
}

.tile-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.tile-head h2 { margin: 0 0 0 2px; font-size: 1.1rem; font-weight: 600; }

.status {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0;
  font-size: 1rem;
  white-space: nowrap;
}
.status .name { font-weight: 600; }
.status .badge-text { color: var(--text-2); transition: color 0.25s; }

/* Grey at rest, white when open/held, Tesla blue while moving or unconfirmed */
[data-state="open"] .status .badge-text,
[data-state="held"] .status .badge-text { color: var(--text); }
[data-state="opening"] .status .badge-text,
[data-state="closing"] .status .badge-text { color: var(--blue-text); }
[data-pending] .status .badge-text { color: var(--blue-text); animation: pending 1s ease-in-out infinite; }

/* ---------- Garage façade ---------- */

.facade {
  --side: 36px;            /* brick either side of the doors */
  --pier: 30px;            /* brick between the doors */
  position: relative;
  display: grid;
  /* Drawing widths incl. trim (art.js: 216+12 and 108+12), so the door openings are exactly 2:1
     and both doors come out the same height */
  grid-template-columns: 228fr 120fr;
  column-gap: var(--pier);
  padding: 0 var(--side);
}

/* Wall behind the doors: roofline, fascia, brick courses, apron (monochrome) */
.facade::before {
  content: "";
  grid-row: 1;
  grid-column: 1 / -1;
  margin: 0 calc(-1 * var(--side));
  border-radius: 8px 8px 0 0;
  border-bottom: 6px solid var(--apron);
  background:
    linear-gradient(var(--roof), var(--roof)) top / 100% 8px no-repeat,
    linear-gradient(var(--fascia), var(--fascia)) 0 8px / 100% 2px no-repeat,
    var(--wall-bg) 0 10px / 26px 10px;
}

.door { display: contents; }
.door[data-device="door18"] > * { grid-column: 1; }
.door[data-device="door9"] > * { grid-column: 2; }

.bay {
  grid-row: 1;
  position: relative;
  z-index: 1;
  padding: 22px 0 6px;     /* roof + fascia + brick above, apron below */
}
.bay svg { display: block; width: 100%; height: auto; }

.door-controls {
  grid-row: 2;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 8px;
}

/* Door artwork (monochrome) */
.bay .trim { fill: var(--trim); }
.bay .interior { fill: var(--interior); }
.bay .floor { fill: var(--floor); }
.bay .glow-stop { stop-color: var(--lit); }
.bay .interior-glow { opacity: 0; transition: opacity 0.4s; }
.bay .slab-face { fill: var(--slab); }
.bay .joint { stroke: var(--joint); stroke-width: 0.8; }
.bay .window { fill: var(--glass); stroke: var(--panel-line); stroke-width: 0.8; transition: fill 0.4s; }
.bay .panel { fill: none; stroke: var(--panel-line); stroke-width: 0.9; }
.bay .panel-inner { fill: var(--panel-fill); stroke: var(--panel-line); stroke-width: 0.5; }
.bay .seal { stroke: var(--seal); stroke-width: 1.6; transition: stroke 0.25s; }

/* Door travel. Transitions only after the first snapshot (html.ready) so a page opened while a
   door is already open doesn't replay a 17 s animation. */
.bay .slab { transform: translateY(0); }
.ready .bay .slab { transition: transform 0.7s cubic-bezier(.3, .7, .2, 1); }
.door[data-state="open"] .slab,
.door[data-state="opening"] .slab { transform: translateY(-74px); }
.door[data-state="closing"] .slab { transform: translateY(-3px); }
.ready .door[data-state="opening"] .slab,
.ready .door[data-state="closing"] .slab { transition: transform var(--door-travel) linear; }
.door[data-state="opening"] .seal,
.door[data-state="closing"] .seal { stroke: var(--blue); stroke-width: 2.4; }
.door[data-state="unknown"] .bay { opacity: 0.45; }

/* Garage lights: the windows (and the open bay) light up, like the real thing */
[data-lights="on"] .bay .window { fill: var(--lit); }
[data-lights="on"] .bay .interior-glow { opacity: 0.6; }

/* Lights toggle in the tile header */
.pill {
  min-height: 44px;
  padding: 0 40px 0 14px;
  border-radius: 999px;
  gap: 8px;
  font-size: 1rem;
}
.pill-bulb { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.pill .badge-text { color: var(--text-2); font-weight: 500; }
.lights[data-state="on"] .pill { background: var(--blue); color: #fff; }
.lights[data-state="on"] .pill:active, .lights[data-state="on"] .pill.pressed { background: #3359c4; }
.lights[data-state="on"] .pill .badge-text { color: rgba(255, 255, 255, 0.85); }
.lights[data-state="on"] .pill-bulb path:first-child { fill: #fff; }
.lights[data-pending] .pill { animation: pending 1s ease-in-out infinite; }

/* ---------- Gate ---------- */

.tile.gate {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 240px;
  gap: 14px;
}
.gate-scene { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.gate-scene .status { padding-left: 2px; }
.gate-art { flex: 1; min-height: 0; display: flex; align-items: flex-end; justify-content: center; }
.gate-art svg { display: block; width: 100%; max-height: 124px; }

.gate-art .ground { stroke: var(--line); stroke-width: 2; }
.gate-art .iron { fill: none; stroke: var(--art-line); stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }
.gate-art .scrolls { stroke-width: 1; }
.gate-art .spear { fill: var(--art-line); }
.gate-art .brick-bg { fill: var(--masonry); }
.gate-art .brick-line { stroke: var(--masonry-line); stroke-width: 0.5; }
.gate-art .masonry { fill: url(#brick); }
.gate-art .cap { fill: var(--cap); }
.gate-art .coping { fill: none; stroke: var(--cap); stroke-width: 3.5; stroke-linecap: round; }
.gate-art .fence { fill: none; stroke: var(--art-line); stroke-width: 3.2; stroke-linecap: square; }
.gate-art .lantern-frame { fill: var(--art-line); }
.gate-art .lantern-glass { fill: var(--lit); stroke: var(--art-line); stroke-width: 1.2; stroke-linejoin: round; }
.gate-art .lantern-line { stroke: var(--art-line); stroke-width: 1; }

/* Swinging open: seen from the front, each leaf narrows toward its hinge pillar. The gate has no
   "moving" feedback (it reports open ~1 s after starting), so open/held animate over the real
   15 s travel; closing uses the "Closing…" prediction and settles when the closed limit trips. */
.gate-art .leaf { transform-box: fill-box; transform: scaleX(1); }
.gate-art .leaf-left { transform-origin: left center; }
.gate-art .leaf-right { transform-origin: right center; }
.ready .gate-art .leaf { transition: transform 1.2s cubic-bezier(.3, .7, .2, 1); }
.gate[data-state="open"] .leaf,
.gate[data-state="held"] .leaf,
.gate[data-state="opening"] .leaf { transform: scaleX(0.14); }
.gate[data-state="closing"] .leaf { transform: scaleX(0.92); }
.ready .gate[data-state="open"] .leaf,
.ready .gate[data-state="held"] .leaf,
.ready .gate[data-state="opening"] .leaf,
.ready .gate[data-state="closing"] .leaf { transition: transform var(--gate-travel) linear; }
.gate[data-state="unknown"] .gate-art { opacity: 0.45; }

.gate-actions { display: flex; flex-direction: column; justify-content: center; gap: 8px; }
.gate-actions .action { min-height: clamp(44px, 7.4vh, 64px); }

/* ---------- Buttons ---------- */

.action {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: clamp(46px, 7.8vh, 66px);
  padding: 0 40px;
  border: 0;
  border-radius: 12px;
  background: var(--control);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.action.pressed, .action:active { background: var(--control-down); transform: scale(0.98); }
.action:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

/* Latched (Keep Open while held): filled Tesla blue like an enabled car control */
.action[aria-pressed="true"] { background: var(--blue); color: #fff; }
.action[aria-pressed="true"].pressed, .action[aria-pressed="true"]:active { background: #3359c4; }

/* Spinner → check / cross inside the button */
.action-status {
  position: absolute;
  right: 14px;
  top: 50%;
  width: 20px;
  height: 20px;
  margin-top: -10px;
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.15s;
}
.action[data-phase="sending"] .action-status {
  opacity: 1;
  border: 2.5px solid rgba(127, 127, 127, 0.35);
  border-top-color: currentColor;
  animation: spin 0.7s linear infinite;
}
.action[data-phase="ok"] .action-status { opacity: 1; }
.action[data-phase="ok"] .action-status::after {
  content: "";
  position: absolute;
  left: 6px;
  top: 2px;
  width: 6px;
  height: 11px;
  border: solid var(--blue-text);
  border-width: 0 2.5px 2.5px 0;
  transform: rotate(45deg);
}
.action[aria-pressed="true"][data-phase="ok"] .action-status::after,
.lights[data-state="on"] .pill[data-phase="ok"] .action-status::after { border-color: #fff; }
.action[data-phase="err"] { animation: shake 0.4s; }
.action[data-phase="err"] .action-status { opacity: 1; }
.action[data-phase="err"] .action-status::before,
.action[data-phase="err"] .action-status::after {
  content: "";
  position: absolute;
  left: 9px;
  top: 2px;
  width: 2.5px;
  height: 16px;
  border-radius: 2px;
  background: var(--red);
}
.action[data-phase="err"] .action-status::before { transform: rotate(45deg); }
.action[data-phase="err"] .action-status::after { transform: rotate(-45deg); }

/* ---------- Notifications (top centre, like car alerts) ---------- */

.toasts {
  position: fixed;
  top: 12px;
  left: 50%;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transform: translateX(-50%);
  pointer-events: none;
}
.toast {
  padding: 12px 20px;
  border-radius: 12px;
  background: var(--control);
  color: var(--text);
  font-size: 1rem;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  animation: toast-in 0.2s ease-out;
}
.toast.err { box-shadow: inset 4px 0 0 var(--red), 0 8px 24px rgba(0, 0, 0, 0.35); }
.toast.warn { box-shadow: inset 4px 0 0 var(--text-2), 0 8px 24px rgba(0, 0, 0, 0.35); }
.toast.leaving { opacity: 0; transition: opacity 0.3s; }

/* ---------- Lock screen ---------- */

.lock {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: var(--bg);
}
.lock[hidden] { display: none; }
.lock-box { max-width: 460px; padding: 24px; color: var(--text-2); text-align: center; }
.lock-box svg { width: 52px; height: 52px; fill: none; stroke: var(--text); stroke-width: 1.8; }
.lock-box h2 { margin: 14px 0 8px; color: var(--text); font-weight: 600; }

/* ---------- Animations ---------- */

@keyframes pulse { 50% { opacity: 0.3; } }
@keyframes pending { 50% { opacity: 0.45; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes shake { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }
