  :root {
    --ink: #15171a;
    --ink-raised: #1c1f23;
    --paper: #eceef0;
    --paper-raised: #ffffff;
    --line: rgba(140, 122, 94, 0.28);
    --line-strong: rgba(140, 122, 94, 0.5);
    --text: #e9e7e2;
    --text-dim: #9ca0a5;
    --text-faint: #6b6f74;
    --brass: #8c7a5e;
    --ember: #e8703a;
    --ember-dim: rgba(232, 112, 58, 0.16);
    --frost: #3fa9c9;
    --frost-dim: rgba(63, 169, 201, 0.16);
    --good: #4c9a6a;
    --alert: #d64545;
    --radius: 7px;
    --mono: "JetBrains Mono", "SF Mono", "Menlo", "Consolas", "Liberation Mono", monospace;
    --sans: "IBM Plex Sans", -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: dark;
  }

  @media (prefers-color-scheme: light) {
    :root {
      --ink: #eceef0;
      --ink-raised: #ffffff;
      --paper: #eceef0;
      --paper-raised: #ffffff;
      --line: rgba(140, 122, 94, 0.32);
      --line-strong: rgba(120, 104, 78, 0.55);
      --text: #1c1e21;
      --text-dim: #5b5e63;
      --text-faint: #8b8e93;
      --ember: #c25a20;
      --frost: #1f7f9e;
      --ember-dim: rgba(232, 112, 58, 0.1);
      --frost-dim: rgba(63, 169, 201, 0.1);
      color-scheme: light;
    }
  }

  :root[data-theme="dark"] {
    --ink: #15171a;
    --ink-raised: #1c1f23;
    --line: rgba(140, 122, 94, 0.28);
    --line-strong: rgba(140, 122, 94, 0.5);
    --text: #e9e7e2;
    --text-dim: #9ca0a5;
    --text-faint: #6b6f74;
    --ember-dim: rgba(232, 112, 58, 0.16);
    --frost-dim: rgba(63, 169, 201, 0.16);
    color-scheme: dark;
  }

  :root[data-theme="light"] {
    --ink: #eceef0;
    --ink-raised: #ffffff;
    --line: rgba(140, 122, 94, 0.32);
    --line-strong: rgba(120, 104, 78, 0.55);
    --text: #1c1e21;
    --text-dim: #5b5e63;
    --text-faint: #8b8e93;
    --ember: #c25a20;
    --frost: #1f7f9e;
    --ember-dim: rgba(232, 112, 58, 0.1);
    --frost-dim: rgba(63, 169, 201, 0.1);
    color-scheme: light;
  }

  * { box-sizing: border-box; }

  body {
    margin: 0;
    background: var(--ink);
    color: var(--text);
    font-family: var(--sans);
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
  }

  ::selection { background: var(--ember); color: #100c08; }

  button, input, select {
    font-family: inherit;
    color: inherit;
  }

  .app-shell {
    display: grid;
    grid-template-columns: 64px 1fr;
    min-height: 100vh;
  }

  /* ---------- Rail ---------- */
  .rail {
    background: var(--ink-raised);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 18px 0;
    gap: 22px;
  }

  .rail-mark {
    width: 30px;
    height: 30px;
    display: grid;
    place-items: center;
  }

  .rail-mark svg { width: 20px; height: 20px; }

  .rail-nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 6px;
  }

  .rail-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: none;
    border-radius: var(--radius);
    display: grid;
    place-items: center;
    cursor: pointer;
    color: var(--text-faint);
    position: relative;
  }

  .rail-btn svg { width: 18px; height: 18px; }

  .rail-btn:hover { color: var(--text); background: rgba(140, 122, 94, 0.1); }

  .rail-btn.is-active { color: var(--ember); }

  .rail-btn.is-active::before {
    content: "";
    position: absolute;
    left: -12px;
    top: 8px;
    bottom: 8px;
    width: 2px;
    background: var(--ember);
    border-radius: 2px;
  }

  .rail-bottom {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  .rail-theme-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid var(--line);
    background: none;
    color: var(--text-faint);
    display: grid;
    place-items: center;
    cursor: pointer;
  }
  .rail-theme-btn svg { width: 15px; height: 15px; }

  /* ---------- Main ---------- */
  .main {
    padding: 28px 36px 80px;
    max-width: 1180px;
  }

  .topbar {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 22px;
  }

  .crumb {
    display: flex;
    align-items: baseline;
    gap: 10px;
    font-family: var(--mono);
  }

  .crumb-back {
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    font-family: var(--mono);
    font-size: 13px;
    letter-spacing: 0.02em;
    padding: 0;
    display: none;
    align-items: center;
    gap: 6px;
  }
  .crumb-back svg { width: 12px; height: 12px; }
  .crumb-back:hover { color: var(--text); }

  h1.view-title {
    font-family: var(--mono);
    font-weight: 700;
    font-size: 22px;
    letter-spacing: -0.01em;
    margin: 0;
    text-wrap: balance;
  }

  .view-sub {
    font-size: 12.5px;
    color: var(--text-faint);
    font-family: var(--mono);
  }

  .topbar-actions { display: flex; gap: 10px; align-items: center; }

  .btn {
    font-family: var(--mono);
    font-size: 12px;
    letter-spacing: 0.03em;
    padding: 8px 14px;
    border-radius: var(--radius);
    border: 1px solid var(--line);
    background: var(--ink-raised);
    color: var(--text-dim);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 7px;
  }
  .btn svg { width: 13px; height: 13px; }
  .btn:hover { border-color: var(--line-strong); color: var(--text); }

  .btn-primary {
    background: var(--ember);
    border-color: var(--ember);
    color: #1a0f08;
    font-weight: 600;
  }
  .btn-primary:hover { color: #1a0f08; filter: brightness(1.08); }

  .btn.is-on {
    border-color: var(--ember);
    color: var(--ember);
  }

  /* ---------- Status strip ---------- */
  .status-strip {
    display: flex;
    gap: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--ink-raised);
    margin-bottom: 26px;
    overflow: hidden;
  }

  .status-cell {
    flex: 1;
    padding: 13px 18px;
    border-right: 1px solid var(--line);
  }
  .status-cell:last-child { border-right: none; }

  .status-label {
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-faint);
    margin-bottom: 5px;
  }

  .status-value {
    font-family: var(--mono);
    font-size: 19px;
    font-variant-numeric: tabular-nums;
    font-weight: 700;
  }
  .status-value.accent-ember { color: var(--ember); }
  .status-value.accent-frost { color: var(--frost); }
  .status-value.accent-good { color: var(--good); }

  /* ---------- Grid ---------- */
  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 14px;
  }

  .card {
    position: relative;
    background: var(--ink-raised);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    cursor: pointer;
    transition: transform 0.14s ease, border-color 0.14s ease, box-shadow 0.14s ease;
  }
  .card::after {
    content: "";
    position: absolute;
    inset: 4px;
    border: 1px solid transparent;
    border-radius: 4px;
    pointer-events: none;
    transition: border-color 0.14s ease;
  }
  .card:hover { transform: translateY(-2px); border-color: var(--line-strong); box-shadow: 0 8px 20px -12px rgba(0,0,0,0.5); }
  .card:hover::after { border-color: rgba(255,255,255,0.04); }
  .card:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }

  .card-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 14px;
  }

  .card-icon {
    width: 30px;
    height: 30px;
    border-radius: 6px;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
  }
  .card-icon svg { width: 15px; height: 15px; }
  .card-icon.tone-ember { background: var(--ember-dim); color: var(--ember); border-color: rgba(232,112,58,0.35); }
  .card-icon.tone-frost { background: var(--frost-dim); color: var(--frost); border-color: rgba(63,169,201,0.35); }
  .card-icon.tone-neutral { background: rgba(140,122,94,0.12); color: var(--brass); }

  .card-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-faint);
    margin-top: 6px;
    flex-shrink: 0;
  }
  .card-dot.is-good { background: var(--good); box-shadow: 0 0 0 3px rgba(76,154,106,0.18); }
  .card-dot.is-ember { background: var(--ember); box-shadow: 0 0 0 3px rgba(232,112,58,0.18); }

  .card-title {
    font-family: var(--mono);
    font-size: 13.5px;
    font-weight: 700;
    letter-spacing: -0.01em;
    margin: 0 0 3px;
    text-wrap: balance;
  }

  .card-meta {
    font-size: 11.5px;
    color: var(--text-faint);
    font-family: var(--mono);
  }

  .card-metric-row {
    display: flex;
    align-items: baseline;
    gap: 4px;
    margin-top: 14px;
  }

  .card-metric {
    font-family: var(--mono);
    font-size: 24px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
  }

  .card-metric-unit {
    font-family: var(--mono);
    font-size: 12px;
    color: var(--text-faint);
  }

  .card-submetric {
    font-family: var(--mono);
    font-size: 11px;
    color: var(--text-dim);
    margin-top: 6px;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
  }

  .card-submetric b { color: var(--text); font-weight: 600; }

  .card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid var(--line);
  }

  .badge {
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: 0.04em;
    color: var(--text-faint);
    display: inline-flex;
    align-items: center;
    gap: 5px;
  }
  .badge svg { width: 11px; height: 11px; }
  .badge.two-way { color: var(--frost); }

  .card-arrow { color: var(--text-faint); }
  .card-arrow svg { width: 14px; height: 14px; }

  .toggle-switch {
    width: 34px;
    height: 19px;
    border-radius: 10px;
    background: rgba(140,122,94,0.22);
    border: 1px solid var(--line);
    position: relative;
    cursor: pointer;
    flex-shrink: 0;
  }
  .toggle-switch::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 13px;
    height: 13px;
    border-radius: 50%;
    background: var(--text-faint);
    transition: transform 0.15s ease, background 0.15s ease;
  }
  .toggle-switch.is-on { background: var(--ember-dim); border-color: rgba(232,112,58,0.4); }
  .toggle-switch.is-on::after { transform: translateX(15px); background: var(--ember); }

  /* device-only widget variants inside group cards */
  .card.type-group .card-title { font-size: 14.5px; }

  /* ---------- Edit mode ---------- */
  .grid.is-editing .card { cursor: grab; }
  .grid.is-editing .card:hover { transform: none; }

  @keyframes wiggle {
    0%, 100% { transform: rotate(-0.6deg); }
    50% { transform: rotate(0.6deg); }
  }
  .grid.is-editing .card:not(.is-dragging) { animation: wiggle 0.26s ease-in-out infinite; }
  @media (prefers-reduced-motion: reduce) {
    .grid.is-editing .card:not(.is-dragging) { animation: none; }
  }

  .card.is-dragging { opacity: 0.35; }
  .card.is-drop-target { border-color: var(--ember); }

  .edit-controls {
    position: absolute;
    top: -8px;
    right: -8px;
    display: none;
    gap: 5px;
  }
  .grid.is-editing .edit-controls { display: flex; }

  .edit-chip {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--ink-raised);
    border: 1px solid var(--line-strong);
    display: grid;
    place-items: center;
    cursor: pointer;
    color: var(--text-dim);
  }
  .edit-chip svg { width: 11px; height: 11px; }
  .edit-chip:hover { color: var(--text); }
  .edit-chip.remove:hover { color: var(--alert); border-color: var(--alert); }

  .drag-handle {
    position: absolute;
    top: 6px;
    left: 6px;
    display: none;
    color: var(--text-faint);
    cursor: grab;
  }
  .grid.is-editing .drag-handle { display: block; }
  .drag-handle svg { width: 12px; height: 12px; }

  .add-tile {
    border: 1px dashed var(--line-strong);
    border-radius: var(--radius);
    display: none;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 8px;
    color: var(--text-faint);
    cursor: pointer;
    min-height: 150px;
    background: none;
  }
  .add-tile svg { width: 20px; height: 20px; }
  .add-tile span { font-family: var(--mono); font-size: 11.5px; letter-spacing: 0.03em; }
  .grid.is-editing .add-tile { display: flex; }
  .add-tile:hover { color: var(--ember); border-color: var(--ember); }

  /* ---------- Modal ---------- */
  .modal-veil {
    position: fixed;
    inset: 0;
    background: rgba(8,7,6,0.55);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 40;
    padding: 20px;
  }
  .modal-veil.is-open { display: flex; }

  .modal {
    width: 100%;
    max-width: 380px;
    background: var(--ink-raised);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius);
    padding: 22px;
    max-height: 86vh;
    overflow-y: auto;
  }

  .modal-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 4px;
  }

  .modal-title {
    font-family: var(--mono);
    font-size: 15px;
    font-weight: 700;
    margin: 0;
  }

  .modal-close {
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    padding: 2px;
  }
  .modal-close svg { width: 15px; height: 15px; }
  .modal-close:hover { color: var(--text); }

  .modal-sub { font-size: 12px; color: var(--text-faint); font-family: var(--mono); margin-bottom: 20px; }

  .field-group { margin-bottom: 18px; }

  .field-label {
    font-family: var(--mono);
    font-size: 10.5px;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--text-faint);
    margin-bottom: 8px;
    display: block;
  }

  .segmented {
    display: flex;
    border: 1px solid var(--line);
    border-radius: 6px;
    overflow: hidden;
  }
  .segmented button {
    flex: 1;
    padding: 7px 0;
    background: none;
    border: none;
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--text-dim);
    cursor: pointer;
    border-right: 1px solid var(--line);
  }
  .segmented button:last-child { border-right: none; }
  .segmented button.is-selected { background: var(--ember-dim); color: var(--ember); font-weight: 700; }

  .checkline {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 7px 0;
    font-size: 13px;
    cursor: pointer;
  }
  .checkline input { accent-color: var(--ember); width: 15px; height: 15px; }

  .modal-actions { display: flex; gap: 10px; margin-top: 6px; }
  .modal-actions .btn { flex: 1; justify-content: center; }

  /* ---------- Picker ---------- */
  .picker-list { display: flex; flex-direction: column; gap: 6px; max-height: 320px; overflow-y: auto; margin-bottom: 4px; }
  .picker-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border: 1px solid var(--line);
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
  }
  .picker-row:hover { border-color: var(--line-strong); }
  .picker-row.is-added { opacity: 0.4; cursor: default; }
  .picker-row .card-icon { width: 24px; height: 24px; }
  .picker-row .card-icon svg { width: 12px; height: 12px; }
  .picker-row-name { flex: 1; font-family: var(--mono); font-size: 12.5px; }
  .picker-row-add { font-family: var(--mono); font-size: 10.5px; color: var(--text-faint); }

  .toast {
    position: fixed;
    bottom: 22px;
    left: 50%;
    transform: translateX(-50%) translateY(0);
    background: var(--ink-raised);
    border: 1px solid var(--line-strong);
    padding: 10px 18px;
    border-radius: var(--radius);
    font-family: var(--mono);
    font-size: 12.5px;
    z-index: 60;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
  }
  .toast.is-shown { opacity: 1; transform: translateX(-50%) translateY(-6px); }

  /* ---------- Plan View ---------- */
  .plan-view { display: none; }
  .plan-view.is-shown { display: block; }

  .plan-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 14px;
    flex-wrap: wrap;
  }

  .plan-hint {
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--text-faint);
  }

  .plan-canvas-frame {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--ink-raised);
    padding: 14px;
    overflow-x: auto;
  }

  .plan-canvas {
    position: relative;
    width: 100%;
    min-width: 720px;
    border-radius: 4px;
    overflow: hidden;
    line-height: 0;
  }

  .plan-image {
    width: 100%;
    display: block;
    filter: saturate(0.82) brightness(0.97);
    user-select: none;
    -webkit-user-drag: none;
  }
  :root[data-theme="light"] .plan-image { filter: saturate(0.9) brightness(1); }
  @media (prefers-color-scheme: light) {
    .plan-image { filter: saturate(0.9) brightness(1); }
  }

  .plan-pin {
    position: absolute;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 4px 9px 4px 4px;
    border-radius: 999px;
    background: var(--ink-raised);
    border: 1.5px solid var(--line-strong);
    cursor: grab;
    box-shadow: 0 4px 14px -6px rgba(0,0,0,0.55);
    touch-action: none;
    max-width: max-content;
  }
  .plan-pin:active { cursor: grabbing; }
  .plan-pin.is-dragging { z-index: 30; box-shadow: 0 10px 26px -8px rgba(0,0,0,0.6); }
  .plan-pin.tone-ember { border-color: var(--ember); }
  .plan-pin.tone-frost { border-color: var(--frost); }

  .plan-pin-icon {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    flex-shrink: 0;
  }
  .plan-pin-icon svg { width: 12px; height: 12px; }
  .plan-pin-icon.tone-ember { background: var(--ember); color: #1a0f08; }
  .plan-pin-icon.tone-frost { background: var(--frost); color: #06222a; }
  .plan-pin-icon.tone-neutral { background: var(--brass); color: #1a1712; }

  .plan-pin-stat {
    font-family: var(--mono);
    font-size: 11.5px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text);
    white-space: nowrap;
  }

  .plan-pin-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-faint);
    flex-shrink: 0;
  }
  .plan-pin-dot.is-on { background: var(--good); box-shadow: 0 0 0 2px rgba(76,154,106,0.2); }

  .plan-popover {
    position: fixed;
    display: none;
    width: 230px;
    background: var(--ink-raised);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius);
    padding: 14px;
    z-index: 50;
    box-shadow: 0 16px 40px -12px rgba(0,0,0,0.5);
  }
  .plan-popover.is-open { display: block; }

  .plan-popover-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
  .plan-popover-name { font-family: var(--mono); font-weight: 700; font-size: 13px; text-wrap: balance; }
  .plan-popover-room { font-family: var(--mono); font-size: 10.5px; color: var(--text-faint); margin-top: 2px; }
  .plan-popover-close { background: none; border: none; color: var(--text-faint); cursor: pointer; flex-shrink: 0; }
  .plan-popover-close svg { width: 13px; height: 13px; }

  .plan-popover-stats { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
  .plan-popover-stat-row { display: flex; justify-content: space-between; font-size: 12px; }
  .plan-popover-stat-row b { font-family: var(--mono); font-variant-numeric: tabular-nums; }

  .plan-popover-actions { display: flex; gap: 8px; }
  .plan-popover-actions .btn { flex: 1; justify-content: center; padding: 7px 0; }

  @media (max-width: 640px) {
    .app-shell { grid-template-columns: 52px 1fr; }
    .main { padding: 20px 16px 70px; }
    .status-strip { flex-wrap: wrap; }
    .status-cell { flex: 1 1 45%; border-bottom: 1px solid var(--line); }
  }
