/* Jarvis web app. No build step, no external fonts or assets. Light/dark via prefers-color-scheme.
   Layouts: phone (bottom tab bar, 16px gutter), desktop/tablet (tabs in the header), panel (?mode=panel). */

:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --surface-3: #e3e7ee;
  --text: #15181d;
  --muted: #5b6573;
  --line: #e1e5eb;
  --accent: #3159d8;
  --accent-ink: #ffffff;
  --accent-soft: #e5ebfc;
  --on: #e6a118;
  --on-soft: #fff3d6;
  --on-ink: #5a3d00;
  --ok: #1d8a57;
  --warn: #b56d00;
  --crit: #cf2f35;
  --info: #3159d8;
  --warn-soft: #fff1d9;
  --crit-soft: #fde6e6;
  --info-soft: #e5ebfc;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 6px 20px rgba(15, 23, 42, .06);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, .18);
  --radius: 18px;
  --radius-sm: 12px;
  --gutter: 16px;
  --tabbar-h: 0px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1114;
    --surface: #171b21;
    --surface-2: #1f242c;
    --surface-3: #2a3039;
    --text: #e9ecf0;
    --muted: #9aa4b2;
    --line: #2a3038;
    --accent: #8aa6ff;
    --accent-ink: #0e1114;
    --accent-soft: #1f2a46;
    --on: #f2b544;
    --on-soft: #3a2e14;
    --on-ink: #ffe2a6;
    --ok: #4cc38a;
    --warn: #f0a93b;
    --crit: #ff6b6b;
    --info: #8aa6ff;
    --warn-soft: #3a2c12;
    --crit-soft: #3d1a1c;
    --info-soft: #1f2a46;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
    --shadow-lg: 0 10px 40px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html.panel { font-size: 21px; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
html.panel body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
html.panel textarea, html.panel input { -webkit-user-select: text; user-select: text; }

[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 1.25rem; height: 1.25rem; flex: none; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.row { display: flex; gap: .625rem; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

/* ------------------------------------------------------------------ buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
  min-height: 44px; padding: .5rem 1.1rem; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: color-mix(in srgb, var(--accent) 88%, var(--text)); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.small { min-height: 38px; padding: .35rem .85rem; font-size: .9rem; }
.btn.block { width: 100%; }
.btn svg { width: 1.1rem; height: 1.1rem; }
.icon-btn {
  position: relative; display: inline-grid; place-items: center;
  width: 44px; height: 44px; border-radius: 999px; border: 0; background: transparent; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }

/* ------------------------------------------------------------------ login */
.login { min-height: 100%; display: grid; place-items: center; padding: calc(var(--gutter) + var(--safe-t)) var(--gutter) calc(var(--gutter) + var(--safe-b)); }
.login-card {
  width: min(400px, 100%); background: var(--surface); border-radius: 24px; padding: 2rem 1.5rem;
  box-shadow: var(--shadow); display: grid; gap: .9rem; text-align: center;
}
.login-card h1 { margin: 0; font-size: 1.6rem; letter-spacing: -.01em; }
.login-card p { margin: 0; }
.login-card .logo { justify-self: center; width: 64px; height: 64px; border-radius: 18px; }
.login-card .logo svg { width: 40px; height: 40px; }
.login-card input {
  width: 100%; min-height: 50px; padding: .75rem 1rem; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--surface-2); font-size: 1rem;
}
.form-error { color: var(--crit); min-height: 1.5em; font-size: .9rem; }

.logo {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(#2b46a6, #18224d); color: #a9bfff;
}
.logo svg { width: 22px; height: 22px; }

/* ------------------------------------------------------------------ app shell */
.app {
  height: 100%; height: 100dvh; display: grid; grid-template-rows: auto auto 1fr;
  padding-left: var(--safe-l); padding-right: var(--safe-r);
}
.topbar {
  display: flex; align-items: center; gap: .75rem;
  padding: calc(.5rem + var(--safe-t)) var(--gutter) .5rem;
  background: var(--bg);
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.15rem; letter-spacing: -.01em; }
.clock { display: none; font-variant-numeric: tabular-nums; font-weight: 600; font-size: 1.6rem; letter-spacing: -.02em; }
.tabs { display: flex; gap: .25rem; margin-left: auto; background: var(--surface-2); padding: 4px; border-radius: 999px; }
.tab {
  display: inline-flex; align-items: center; gap: .45rem; min-height: 40px; padding: .35rem 1rem;
  border: 0; border-radius: 999px; background: transparent; color: var(--muted); font-weight: 600;
}
.tab[aria-current="page"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.alerts-btn { margin-left: .25rem; }
.badge {
  position: absolute; top: 3px; right: 1px; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  display: grid; place-items: center; font-size: .72rem; font-weight: 700; line-height: 1;
  background: var(--info); color: #fff; border: 2px solid var(--bg);
}
.badge.warning { background: var(--warn); }
.badge.critical { background: var(--crit); }

/* status strip */
.status-strip {
  display: flex; gap: .5rem; padding: 0 var(--gutter) .5rem; overflow-x: auto; scrollbar-width: none;
}
.status-strip::-webkit-scrollbar { display: none; }
.pill {
  display: inline-flex; align-items: center; gap: .45rem; white-space: nowrap;
  padding: .3rem .75rem; border-radius: 999px; background: var(--surface); border: 1px solid var(--line);
  font-size: .82rem; color: var(--muted);
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); opacity: .6; }
.pill.ok .dot { background: var(--ok); opacity: 1; }
.pill.warn .dot { background: var(--warn); opacity: 1; }
.pill.bad .dot { background: var(--crit); opacity: 1; }
.pill.bad { color: var(--crit); border-color: color-mix(in srgb, var(--crit) 35%, var(--line)); }
.pill b { color: var(--text); font-weight: 600; }

.main { min-height: 0; position: relative; }
.view { height: 100%; min-height: 0; }

/* ------------------------------------------------------------------ chat */
.chat-view { display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; justify-content: flex-end; gap: .5rem;
  width: 100%; max-width: 820px; margin: 0 auto; padding: 0 var(--gutter);
}
.chat-close { display: none; }
.messages {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: .5rem var(--gutter) 1rem; scroll-padding-bottom: 1rem;
}
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.chat-empty { padding: 12vh 0 2rem; text-align: center; }
.greeting { font-size: 2rem; font-weight: 700; letter-spacing: -.02em; margin: 0 0 .25rem; }
.suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: .5rem; margin-top: 1.25rem; }
.chip-btn {
  min-height: 44px; padding: .5rem 1rem; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.chip-btn:hover { border-color: var(--accent); }

.msg { margin-top: 1rem; display: flex; flex-direction: column; gap: .5rem; }
.msg.user { align-items: flex-end; }
.msg.user .bubble {
  max-width: min(85%, 640px); background: var(--accent); color: var(--accent-ink);
  padding: .65rem 1rem; border-radius: 20px 20px 6px 20px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .md { overflow-wrap: anywhere; font-size: 1.05rem; }
.md p { margin: 0 0 .6rem; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0 0 .6rem; padding-left: 1.4rem; }
.md li { margin: .15rem 0; }
.md code { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: .1em .35em; border-radius: 6px; }
.md .md-h { font-weight: 700; }
.thinking { display: inline-flex; gap: 5px; padding: .5rem 0; }
.thinking i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.thinking i:nth-child(2) { animation-delay: .15s; }
.thinking i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

.tools { display: flex; flex-wrap: wrap; gap: .4rem; }
.tools:empty { display: none; }
.tool-chip {
  display: inline-flex; align-items: center; gap: .4rem; max-width: 100%;
  padding: .3rem .75rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: .85rem;
}
.tool-chip .spin {
  width: 12px; height: 12px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent;
  animation: spin .8s linear infinite;
}
.tool-chip svg { width: 14px; height: 14px; }
.tool-chip.ok { color: var(--ok); }
.tool-chip.fail { color: var(--crit); background: var(--crit-soft); }
.tool-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@keyframes spin { to { transform: rotate(360deg); } }

.confirm-card, .error-card {
  border-radius: var(--radius); padding: 1rem; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.confirm-card p { margin: 0 0 .75rem; font-weight: 600; }
.confirm-card .btn { min-width: 96px; }
.confirm-card.answered .btn { display: none; }
.error-card { background: var(--crit-soft); border-color: transparent; color: var(--crit); box-shadow: none; }

.composer {
  display: flex; align-items: flex-end; gap: .5rem;
  width: 100%; max-width: 820px; margin: 0 auto;
  padding: .5rem var(--gutter) calc(.75rem + var(--tabbar-h) + var(--safe-b));
}
.composer textarea {
  flex: 1; min-height: 56px; max-height: 40vh; resize: none; padding: .95rem 1.15rem; border-radius: 28px;
  border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow);
  font-size: 1.08rem; line-height: 1.4;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.send-btn {
  width: 56px; height: 56px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); flex: none; box-shadow: var(--shadow);
}
.send-btn:disabled { opacity: .45; }
.send-btn svg { width: 24px; height: 24px; }

/* ------------------------------------------------------------------ rooms */
.rooms-view { overflow-y: auto; overscroll-behavior: contain; }
.rooms-grid {
  display: grid; gap: var(--gutter); grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  padding: .5rem var(--gutter) calc(1.5rem + var(--tabbar-h) + var(--safe-b));
  max-width: 1400px; margin: 0 auto; align-items: start;
}
.room {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem;
  display: flex; flex-direction: column; gap: .85rem;
}
.room-head { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; }
.room-head h2 { margin: 0; font-size: 1.15rem; letter-spacing: -.01em; }
.readings { display: flex; gap: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.reading { display: inline-flex; align-items: center; gap: .25rem; white-space: nowrap; }
.reading b { color: var(--text); font-size: 1.15rem; font-weight: 650; }
.reading svg { width: 1rem; height: 1rem; }
.climate-line { font-size: .9rem; color: var(--muted); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(124px, 1fr)); gap: .6rem; }
.tile {
  position: relative; display: flex; flex-direction: column; align-items: flex-start; gap: .4rem; text-align: left;
  min-height: 88px; padding: .75rem .85rem; border-radius: var(--radius-sm); border: 0;
  background: var(--surface-2); transition: background .15s, transform .1s;
}
.tile:active { transform: scale(.97); }
.tile svg { width: 1.5rem; height: 1.5rem; color: var(--muted); }
.tile .t-name { font-weight: 600; font-size: .95rem; line-height: 1.25; overflow-wrap: anywhere; }
.tile .t-state { font-size: .8rem; color: var(--muted); }
.tile[aria-pressed="true"] { background: var(--on-soft); }
.tile[aria-pressed="true"] svg { color: var(--on); }
.tile[aria-pressed="true"] .t-state { color: var(--on-ink); }
.tile.busy { opacity: .6; }
.tile:disabled { cursor: default; opacity: .5; }
.rooms-empty { grid-column: 1 / -1; text-align: center; padding: 3rem 1rem; color: var(--muted); }

/* ------------------------------------------------------------------ alerts sheet */
.scrim { position: fixed; inset: 0; background: rgba(10, 12, 16, .35); z-index: 30; animation: fade .15s; }
.sheet {
  position: fixed; z-index: 40; top: 0; right: 0; bottom: 0; width: min(440px, 100%);
  background: var(--bg); box-shadow: var(--shadow-lg); display: flex; flex-direction: column;
  padding-top: var(--safe-t); padding-bottom: var(--safe-b); animation: slide-in .2s ease-out;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: .75rem var(--gutter); }
.sheet-head h2 { margin: 0; font-size: 1.3rem; }
.alerts-list { overflow-y: auto; padding: 0 var(--gutter) var(--gutter); display: grid; gap: .6rem; align-content: start; }
.alert-item {
  border-radius: var(--radius-sm); padding: .85rem 1rem; background: var(--surface); border-left: 5px solid var(--info);
  box-shadow: var(--shadow);
}
.alert-item.warning { border-left-color: var(--warn); }
.alert-item.critical { border-left-color: var(--crit); background: var(--crit-soft); }
.alert-item h3 { margin: 0 0 .2rem; font-size: 1rem; }
.alert-item p { margin: 0; color: var(--muted); font-size: .92rem; white-space: pre-line; }
.alert-item .when { font-size: .78rem; color: var(--muted); margin-top: .35rem; }
.all-clear { text-align: center; color: var(--muted); padding: 3rem 1rem; }
.all-clear svg { width: 2.5rem; height: 2.5rem; color: var(--ok); }
@keyframes slide-in { from { transform: translateX(30px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

/* ------------------------------------------------------------------ dialog, toast, fab */
.dialog {
  border: 0; border-radius: 22px; padding: 1.25rem; width: min(420px, calc(100% - 2 * var(--gutter)));
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
.dialog::backdrop { background: rgba(10, 12, 16, .4); }
.dialog h2 { margin: 0 0 .5rem; font-size: 1.2rem; }
.dialog p { margin: 0 0 1.1rem; color: var(--muted); }
.toast {
  position: fixed; z-index: 50; left: 50%; transform: translateX(-50%);
  bottom: calc(1rem + var(--tabbar-h) + var(--safe-b)); width: max-content; max-width: calc(100% - 2 * var(--gutter));
  padding: .7rem 1.1rem; border-radius: 14px; background: var(--text); color: var(--bg);
  box-shadow: var(--shadow-lg); font-weight: 500; animation: fade .2s;
}
.toast.error { background: var(--crit); color: #fff; }
.fab { display: none; }

/* ------------------------------------------------------------------ phone */
@media (max-width: 719px) {
  :root { --tabbar-h: 64px; }
  .tabs {
    position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; margin: 0; border-radius: 0;
    height: calc(var(--tabbar-h) + var(--safe-b)); padding: 6px max(var(--gutter), var(--safe-l)) var(--safe-b);
    background: var(--bg); background: color-mix(in srgb, var(--bg) 88%, transparent); border-top: 1px solid var(--line);
    -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  }
  .tab { flex: 1; flex-direction: column; gap: 2px; justify-content: center; font-size: .75rem; min-height: 52px; }
  .tab svg { width: 1.45rem; height: 1.45rem; }
  .tab[aria-current="page"] { background: transparent; box-shadow: none; color: var(--accent); }
  .alerts-btn { margin-left: auto; }
  .greeting { font-size: 1.7rem; }
  .chat-empty { padding-top: 8vh; }
  .rooms-grid { grid-template-columns: 1fr; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .sheet { width: 100%; }
}

/* ------------------------------------------------------------------ wall panel (?mode=panel) */
html.panel .clock { display: block; margin-left: .5rem; }
html.panel .tabs { display: none; }
html.panel .alerts-btn { margin-left: auto; width: 56px; height: 56px; }
html.panel .alerts-btn svg { width: 1.6rem; height: 1.6rem; }
html.panel .brand-name { display: none; }
html.panel .logo { width: 44px; height: 44px; }
html.panel .pill { font-size: .8rem; padding: .35rem .9rem; }
html.panel #view-rooms { display: block !important; }
html.panel .rooms-grid { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); padding-bottom: 6rem; }
html.panel .tile { min-height: 104px; }
html.panel #view-chat {
  display: flex !important; position: fixed; z-index: 45; top: 0; right: 0; bottom: 0; width: min(640px, 92vw);
  background: var(--bg); box-shadow: var(--shadow-lg); padding-top: calc(.75rem + var(--safe-t));
  transform: translateX(105%); transition: transform .25s ease; visibility: hidden;
}
html.panel.chat-open #view-chat { transform: none; visibility: visible; }
html.panel .chat-close { display: inline-grid; }
html.panel .chat-head { justify-content: space-between; }
html.panel .fab {
  display: inline-flex; align-items: center; gap: .6rem; position: fixed; z-index: 25;
  right: calc(var(--gutter) + var(--safe-r)); bottom: calc(var(--gutter) + var(--safe-b));
  min-height: 64px; padding: 0 1.6rem; border-radius: 999px; border: 0;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; box-shadow: var(--shadow-lg);
}
html.panel .fab svg { width: 1.5rem; height: 1.5rem; }
html.panel.chat-open .fab { display: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
