:root {
  --bg: #f6f5f3;
  --surface: #ffffff;
  --surface-2: #f0eeeb;
  --text: #1c1917;
  --muted: #78716c;
  --border: #e7e5e4;
  --accent: #ea580c;
  --accent-soft: #fff1e6;
  --on-accent: #ffffff;
  --ok: #16a34a;
  --warn: #b45309;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 16px rgb(0 0 0 / .04);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121110;
    --surface: #1c1a19;
    --surface-2: #262321;
    --text: #f5f5f4;
    --muted: #a8a29e;
    --border: #2f2b29;
    --accent: #fb923c;
    --accent-soft: #3a2314;
    --on-accent: #1c1917;
    --ok: #4ade80;
    --warn: #fbbf24;
    --danger: #f87171;
    --shadow: 0 1px 2px rgb(0 0 0 / .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
h1 { font-size: 22px; margin: 8px 0 0; }
h2 { font-size: 15px; margin: 18px 0 10px; font-weight: 650; }
.card h2:first-child { margin-top: 0; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.logo { fill: var(--accent); stroke: none; width: 22px; height: 22px; }
.logo.big { width: 44px; height: 44px; }

button, input, textarea, select {
  font: inherit; color: inherit;
}
button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; transition: background .15s, border-color .15s, transform .05s;
}
button:hover { border-color: var(--muted); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.link-button { border: 0; background: none; color: var(--muted); padding: 4px 6px; text-decoration: underline; }
.icon-button { padding: 0; width: 42px; height: 42px; display: grid; place-items: center; flex: none; }
input, textarea {
  width: 100%; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 12px; outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 14px; }
.card form, form.card { display: grid; gap: 8px; }
.card form.row { display: flex; }  /* one-line forms: input and button side by side */
.card form.row button { flex: none; }
.error-text { color: var(--danger); min-height: 1.2em; margin: 0; }
.warning-text { color: var(--warn); }

/* Layout */
/* A column: header, optional banner, tabs, then main takes the rest (works whether or not the banner shows). */
.app { display: flex; flex-direction: column; height: 100dvh; max-width: 1100px; margin: 0 auto; }
.app > * { flex: none; }
.app > main { flex: 1; min-height: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px 8px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.status { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.top-right { display: flex; align-items: center; gap: 10px; }
/* Wall-tablet extras: a clock you can read across the room, bigger touch targets. */
.clock { display: flex; flex-direction: column; align-items: center; line-height: 1.1; }
.clock b { font-size: 30px; font-weight: 650; letter-spacing: .02em; font-variant-numeric: tabular-nums; }
.clock span { font-size: 13px; color: var(--muted); }
body.panel .tab-button { padding: 12px 18px; font-size: 16px; }
body.panel .device button, body.panel .list button { min-height: 44px; }
body.panel .slider input { height: 28px; }
.pill { font-size: 12px; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.ok::before, .pill.warn::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.pill.ok::before { background: var(--ok); }
.pill.warn::before { background: var(--warn); }
.banner { margin: 4px 18px 0; padding: 10px 14px; border-radius: 10px; background: var(--accent-soft); border: 1px solid var(--accent); font-size: 14px; }
.tabs { display: flex; gap: 4px; padding: 6px 14px; border-bottom: 1px solid var(--border); }
.tab-button { display: flex; align-items: center; gap: 7px; border: 0; background: none; color: var(--muted); padding: 8px 12px; border-radius: 9px; }
.tab-button.active { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
main { overflow: auto; padding: 16px 18px 24px; }
.tab { display: none; }
.tab.active { display: block; }

/* Chat */
.chat.active { display: flex; flex-direction: column; height: 100%; }
.messages { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 10px; padding-bottom: 12px; min-height: 0; }
.msg { max-width: min(78%, 640px); padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--on-accent); border-bottom-right-radius: 5px; }
.msg.bot { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 5px; }
.msg.error { border-color: var(--danger); color: var(--danger); }
.msg .heard { display: block; font-size: 12px; opacity: .8; margin-bottom: 2px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.action { font-size: 12px; background: var(--surface-2); border-radius: 8px; padding: 3px 8px; color: var(--muted); }
.typing { align-self: flex-start; display: flex; gap: 4px; padding: 14px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25 } 40% { opacity: 1 } }
.empty-chat { margin: auto; text-align: center; color: var(--muted); max-width: 420px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 14px; }
.suggestions button { font-size: 13px; border-radius: 99px; }
.composer { display: flex; gap: 8px; align-items: flex-end; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 8px; box-shadow: var(--shadow); }
.composer textarea { border: 0; box-shadow: none; resize: none; max-height: 160px; padding: 10px 6px; }
#mic.recording { background: var(--danger); border-color: var(--danger); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgb(220 38 38 / .25); } }
.chat-tools { display: flex; align-items: center; gap: 12px; padding: 8px 4px 0; flex-wrap: wrap; }
.chat-tools #reset { margin-left: auto; }
.toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; }
/* Visually hidden but still reachable with the keyboard and screen readers. */
.toggle input, .drop input { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; }
.toggle input:focus-visible + span, .drop:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.toggle span { width: 34px; height: 20px; border-radius: 99px; background: var(--border); position: relative; transition: background .15s; }
.toggle span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle input:checked + span { background: var(--accent); }
.toggle input:checked + span::after { transform: translateX(14px); }

/* Devices */
.device-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.device { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; box-shadow: var(--shadow); }
.device.on { border-color: var(--accent); background: linear-gradient(var(--accent-soft), var(--surface) 70%); }
.device-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.device-name { font-weight: 600; }
.device-room { font-size: 12px; color: var(--muted); }
.badge { font-size: 11px; border-radius: 6px; padding: 2px 6px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.offline { background: var(--danger); color: #fff; }
.badge.risky { color: var(--warn); }
.reading { font-size: 26px; font-weight: 650; }
.reading small { font-size: 14px; color: var(--muted); font-weight: 500; margin-left: 10px; }
.switch { justify-self: start; }
.slider { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.slider input { padding: 0; accent-color: var(--accent); }
.history { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.history li { display: flex; gap: 10px; font-size: 14px; padding: 8px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.history time { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 92px; }
.source { margin-left: auto; font-size: 12px; color: var(--muted); }

/* Knowledge */
.add-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 12px; margin-top: 14px; }
.add-grid .card + .card { margin-top: 0; }
.drop { display: grid; place-items: center; gap: 4px; text-align: center; border: 2px dashed var(--border); border-radius: 12px; padding: 22px 12px; cursor: pointer; }
.drop.over, .drop:hover { border-color: var(--accent); background: var(--accent-soft); }
.drop { position: relative; }
.drop svg { width: 26px; height: 26px; color: var(--accent); }
.results { display: grid; gap: 10px; margin-top: 12px; }
.hit { border-left: 3px solid var(--accent); padding: 4px 12px; }
.hit-source { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.hit-text { white-space: pre-wrap; font-size: 14px; max-height: 9.5em; overflow: hidden; }
.list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.card .list li { background: var(--surface-2); border-color: transparent; }
.list .grow { flex: 1; min-width: 0; }
.list .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .sub { font-size: 12px; color: var(--muted); }
.kind { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; font-weight: 700; color: var(--accent); min-width: 38px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
.list li.paused .title { color: var(--muted); text-decoration: line-through; }
.empty { color: var(--muted); font-size: 14px; padding: 6px 2px; }

/* Settings */
.button-link { display: inline-block; text-decoration: none; border-radius: 10px; padding: 8px 14px; }
.button-link.primary { background: var(--accent); color: var(--on-accent); font-weight: 600; }
.setting { display: grid; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.setting input { font-size: 15px; color: var(--text); }

/* Login, dialog, toast */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px; width: min(340px, 100%); display: grid; gap: 12px; text-align: center; justify-items: center; box-shadow: var(--shadow); }
.login-card input { text-align: center; font-size: 20px; letter-spacing: .3em; }
.login-card button { width: 100%; }
dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text); padding: 0; max-width: min(420px, 92vw); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dialog { padding: 20px; display: grid; gap: 14px; }
.dialog p { margin: 0; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: min(520px, 92vw); font-size: 14px; z-index: 10; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Phones: tabs at the bottom, full-width bubbles */
@media (max-width: 640px) {
  .tabs { order: 4; border-top: 1px solid var(--border); border-bottom: 0; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); background: var(--surface); }
  .tab-button { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 10px; }
  .topbar { padding: 12px 16px 6px; }
  .status .pill:not(.warn) { display: none; }
  main { padding: 12px 16px 16px; }
  .msg { max-width: 88%; }
  .banner { margin: 4px 16px 0; font-size: 13px; padding: 8px 12px; }
  .chat-tools { font-size: 13px; }
  /* iPhone Safari zooms into any text field smaller than 16px. */
  input, textarea, select { font-size: 16px; }
  .history time { min-width: 72px; }
}
