:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --surface: #ffffff;
  --text: #111111;
  --muted: #5c5c60;
  --line: rgba(60, 60, 67, 0.2);
  --accent: #0060df;
  --on-accent: #ffffff;
  --green: #248a3d;
  --warm: #d97706;
  --cool: #0a7ea4;
  --off: #8e8e93;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --tab-h: 56px;
  --sidebar-w: 0px;
  --composer-h: 0px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --text: #ffffff;
    --muted: #a1a1a6;
    --line: rgba(84, 84, 88, 0.6);
    --accent: #4da3ff;
    --on-accent: #00122e;
    --green: #30d158;
    --warm: #fbbf24;
    --cool: #5ac8e6;
    --off: #8a8a8e;
    --danger: #ff8a80;
    --danger-bg: #3b1d1d;
  }
}

* { box-sizing: border-box; }

/* Dynamic Type: the system body style lives on html, so every rem follows the text-size setting */
html {
  -webkit-text-size-adjust: 100%;
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  font: -apple-system-body;
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
}
body.chatting { --composer-h: 68px; }

main {
  margin-inline-start: var(--sidebar-w);
  padding: calc(env(safe-area-inset-top) + 12px) max(16px, env(safe-area-inset-right))
    calc(var(--tab-h) + var(--composer-h) + env(safe-area-inset-bottom) + 20px)
    max(16px, env(safe-area-inset-left));
}
.view { max-width: 720px; margin: 0 auto; }

h1 { font-size: 2rem; line-height: 1.15; margin: 8px 0 18px; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 8px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.note { color: var(--muted); }
bdi { unicode-bidi: isolate; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:active:not(:disabled) { opacity: 0.55; }

/* grouped lists: content layer, opaque, no glass */
.area-title {
  margin: 22px 16px 6px;
  font-size: 0.76rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.group { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 14px; overflow: hidden; }
.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 8px 16px;
}
.row + .row { border-top: 1px solid var(--line); }
.row .icon { flex: none; width: 1.9rem; height: 1.9rem; color: var(--off); display: grid; place-items: center; }
.row .icon svg { width: 1.6rem; height: 1.6rem; }
.row .icon.on { color: var(--on-color, var(--warm)); }
.row .text { flex: 1 1 8rem; min-width: 0; }
.row .name { display: block; overflow-wrap: anywhere; }
.row .sub { display: block; color: var(--muted); font-size: 0.88rem; }
.row .value { color: var(--muted); font-variant-numeric: tabular-nums; }
.row.detail { padding-inline-start: calc(16px + 1.9rem + 12px); }
.row.climate { --on-color: var(--cool); }
.row.detail .label { flex: none; white-space: nowrap; }
.row.detail input[type="range"] { flex: 1 1 5rem; min-width: 5rem; }
.row.detail .range-end { flex: none; white-space: nowrap; }

/* switch: only inside list rows (HIG toggles) */
.switch {
  position: relative;
  flex: none;
  width: 51px;
  height: 31px;
  padding: 0;
  border: 0;
  border-radius: 31px;
  background: var(--off);
  transition: background 0.2s;
}
.switch::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: transform 0.2s;
}
.switch::after { content: ""; position: absolute; inset: -7px -4px; }
.switch[aria-checked="true"] { background: var(--green); }
.switch[aria-checked="true"]::before { transform: translateX(20px); }

input[type="range"] { flex: 1 1 8rem; min-height: 44px; accent-color: var(--warm); }
.range-end { color: var(--muted); font-size: 0.88rem; min-width: 3.5ch; text-align: end; font-variant-numeric: tabular-nums; }

/* two-segment stepper beside the value it changes (HIG steppers) */
.value.big { font-size: 1.6rem; font-weight: 600; color: var(--text); margin-inline-start: auto; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--bg); }
.seg button { width: 52px; min-height: 44px; border: 0; background: none; font-size: 1.4rem; line-height: 1; }
.seg button + button { border-inline-start: 1px solid var(--line); }
.seg button:disabled { opacity: 0.35; cursor: default; }

select { min-height: 44px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }

/* feedback: in context, never auto-dismissed */
#banner {
  position: fixed;
  z-index: 30;
  top: calc(env(safe-area-inset-top) + 8px);
  left: calc(var(--sidebar-w) + 12px);
  right: 12px;
  max-width: 696px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  border-radius: 12px;
  background: var(--danger-bg);
  color: var(--text);
  border: 1px solid var(--danger);
}
#banner[hidden] { display: none; }
#banner span { flex: 1 1 12rem; }
#pending { max-width: 752px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 4px) 16px 0; }
#pending:empty { padding: 0; }
.confirm { margin-top: 8px; padding: 14px 16px; border-radius: 14px; background: var(--surface); border: 2px solid var(--accent); }
.confirm p { margin: 0 0 12px; }
.actions { display: flex; flex-wrap: wrap; gap: 12px; }
.btn, button.primary {
  min-height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  font-weight: 600;
}
.btn.primary, button.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }

/* chat */
.messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 86%; padding: 10px 14px; border-radius: 18px; background: var(--surface); overflow-wrap: anywhere; }
.msg p { margin: 0; white-space: pre-wrap; text-align: start; unicode-bidi: plaintext; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--on-accent); }
.msg.assistant { align-self: flex-start; }
.msg.error { align-self: stretch; max-width: 100%; background: var(--danger-bg); border: 1px solid var(--danger); }
.msg.thinking { align-self: flex-start; color: var(--muted); }
.chips { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); font-size: 0.82rem; color: var(--muted); background: var(--bg); }
.by { margin: 4px 0 0; font-size: 0.78rem; color: var(--muted); }

/* functional layer: glass is for the bars only */
#composer {
  position: fixed;
  z-index: 10;
  left: var(--sidebar-w);
  right: 0;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 8px max(12px, calc((100% - 720px) / 2));
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  -webkit-backdrop-filter: blur(24px) saturate(1.4);
  backdrop-filter: blur(24px) saturate(1.4);
}
#composer[hidden] { display: none; }
#message {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--surface);
  resize: none;
}
#send {
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  display: grid;
  place-items: center;
}
#send:disabled { opacity: 0.5; }

/* activity log */
.log-row { display: block; padding: 10px 16px; }
.log-row .line { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; }
.log-row .when { color: var(--muted); font-size: 0.82rem; }
.log-row .detail-text { color: var(--muted); font-size: 0.88rem; overflow-wrap: anywhere; }
.result { font-weight: 600; }
.result.bad { color: var(--danger); }

/* tab bar: navigation only, always visible, single-word labels; labels keep a fixed size */
#tabs {
  position: fixed;
  z-index: 20;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 78%, transparent);
  -webkit-backdrop-filter: blur(24px) saturate(1.4);
  backdrop-filter: blur(24px) saturate(1.4);
  border-top: 1px solid var(--line);
}
#tabs button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 49px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
#tabs button[aria-current="page"] { color: var(--accent); }

dialog { max-width: 420px; width: calc(100% - 32px); border: 0; border-radius: 16px; background: var(--surface); color: var(--text); padding: 20px; }
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog input { width: 100%; min-height: 44px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); margin: 8px 0 14px; }

/* regular width (tablets, landscape): the tab bar becomes a sidebar, same functions */
@media (min-width: 900px) {
  :root { --sidebar-w: 232px; --tab-h: 0px; }
  #tabs {
    top: 0;
    right: auto;
    width: var(--sidebar-w);
    height: auto;
    flex-direction: column;
    justify-content: flex-start;
    gap: 4px;
    padding: calc(env(safe-area-inset-top) + 20px) 12px 12px;
    border-top: 0;
    border-inline-end: 1px solid var(--line);
  }
  #tabs button { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; padding: 0 14px; border-radius: 12px; font-size: 1rem; }
  #tabs button[aria-current="page"] { background: color-mix(in srgb, var(--accent) 14%, transparent); }
  #pending { max-width: none; margin: 0; margin-inline-start: var(--sidebar-w); padding-inline: 24px; }
  #composer { bottom: 0; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  main { padding-bottom: calc(var(--composer-h) + env(safe-area-inset-bottom) + 24px); }
  #areas { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); column-gap: 20px; align-items: start; }
  .view { max-width: 1080px; }
  #view-chat, #view-log { max-width: 720px; }
}

@media (prefers-contrast: more) {
  :root { --text: #000000; --muted: #3a3a3c; --line: rgba(0, 0, 0, 0.6); --off: #636366; }
  .group, .msg, .chip, .seg, select, #message { border: 1px solid var(--line); }
}
@media (prefers-contrast: more) and (prefers-color-scheme: dark) {
  :root { --text: #ffffff; --muted: #d1d1d6; --line: rgba(255, 255, 255, 0.6); --off: #aeaeb2; }
}
@media (prefers-reduced-transparency: reduce) {
  #tabs, #composer { background: var(--surface); -webkit-backdrop-filter: none; backdrop-filter: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
