:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #17202b;
  --muted: #5d6877;
  --border: #dde1e7;
  --accent: #2463eb;
  --accent-text: #ffffff;
  --ok: #17803d;
  --ok-bg: #e3f5e9;
  --busy: #2463eb;
  --busy-bg: #e5edff;
  --warn: #9a5b00;
  --warn-bg: #fff2d6;
  --bad: #c0262d;
  --bad-bg: #fde6e6;
  --off: #5d6877;
  --off-bg: #eceef1;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #18202a;
    --surface-2: #212b37;
    --text: #e8edf3;
    --muted: #9aa6b4;
    --border: #2a3542;
    --accent: #5b8cff;
    --accent-text: #0b1220;
    --ok: #4cc77a;
    --ok-bg: #143223;
    --busy: #7aa2ff;
    --busy-bg: #18264a;
    --warn: #f0b44c;
    --warn-bg: #3a2c10;
    --bad: #ff7076;
    --bad-bg: #401a1d;
    --off: #9aa6b4;
    --off-bg: #232c37;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button, input, select, textarea { font: inherit; color: inherit; }

[hidden] { display: none !important; }

/* ---------- login ---------- */

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-card img { align-self: center; }
.login-card h1 { margin: 4px 0 10px; text-align: center; font-size: 1.3rem; }
.login-card label { font-size: .9rem; color: var(--muted); }

input[type=password], input[type=number], input[type=text], select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 12px;
}

textarea { resize: vertical; min-height: 96px; }

.voice-controls { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin: 6px 0; }
.voice-button { display: inline-flex; align-items: center; gap: 6px; flex: none; }
.voice-button.listening { color: var(--bad); background: var(--bad-bg); }
.voice-status { flex: 1 1 160px; color: var(--muted); font-size: .82rem; overflow-wrap: anywhere; }

input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.error { color: var(--bad); min-height: 1.2em; margin: 4px 0 0; font-size: .92rem; }

/* ---------- buttons ---------- */

button { cursor: pointer; border: 0; background: none; }

.primary, .secondary, .danger {
  border-radius: 10px;
  padding: 11px 16px;
  font-weight: 600;
  min-height: 44px;
}

.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--surface-2); color: var(--text); }
.danger { background: var(--bad-bg); color: var(--bad); }
.primary:disabled, .secondary:disabled, .danger:disabled { opacity: .5; cursor: default; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.3rem;
  color: var(--muted);
}
.icon-btn:active { background: var(--surface-2); }

.link-btn { color: var(--accent); font-weight: 600; padding: 6px 0; }

/* ---------- layout ---------- */

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top) + 10px) 16px 8px;
  border-bottom: 1px solid var(--border);
}

.top-row { display: flex; align-items: center; justify-content: space-between; }
.top h1 { margin: 0; font-size: 1.35rem; }

.pc-status { font-size: .85rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.pc-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); flex: none; }
.pc-status.ok .dot { background: var(--ok); }
.pc-status.bad { color: var(--bad); }
.pc-status.bad .dot { background: var(--bad); }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 16px calc(env(safe-area-inset-bottom) + 88px);
}

.view { display: flex; flex-direction: column; gap: 12px; }

.tabs {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabs button {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px 10px;
  font-size: .75rem;
  color: var(--muted);
  min-height: 56px;
}

.tabs button.active { color: var(--accent); font-weight: 600; }
.tab-icon { font-size: 1.2rem; line-height: 1.2; }

.badge {
  position: absolute;
  top: 4px;
  left: calc(50% + 8px);
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--bad);
  color: #fff;
  font-size: .7rem;
  font-weight: 700;
  line-height: 18px;
}

/* ---------- cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  min-width: 0;
}

.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.card h2 { margin: 0; font-size: 1.05rem; line-height: 1.3; }
.card .sub { color: var(--muted); font-size: .85rem; margin-top: 2px; }
.summary { margin: 10px 0 0; overflow-wrap: anywhere; }

.chip {
  flex: none;
  font-size: .78rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.tone-ok { color: var(--ok); background: var(--ok-bg); }
.tone-busy { color: var(--busy); background: var(--busy-bg); }
.tone-warn { color: var(--warn); background: var(--warn-bg); }
.tone-bad { color: var(--bad); background: var(--bad-bg); }
.tone-off { color: var(--off); background: var(--off-bg); }

details.more { margin-top: 10px; }
details.more summary { color: var(--accent); font-weight: 600; font-size: .9rem; cursor: pointer; padding: 4px 0; }
details.more ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: .9rem; }
details.more li { margin: 4px 0; overflow-wrap: anywhere; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.actions button { flex: 1 1 120px; }

.pending-note {
  margin-top: 10px;
  font-size: .88rem;
  color: var(--busy);
  background: var(--busy-bg);
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.empty { color: var(--muted); text-align: center; padding: 32px 8px; }

/* ---------- alerts ---------- */

.alerts { max-width: 720px; margin: 0 auto; padding: 0 16px; }
.alert {
  margin-top: 10px;
  background: var(--bad-bg);
  color: var(--bad);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: .92rem;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  justify-content: space-between;
}
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.info { background: var(--busy-bg); color: var(--busy); align-items: center; }
.alert button { color: inherit; font-weight: 700; flex: none; min-height: 32px; }
.alert-actions { display: flex; gap: 12px; flex: none; }

/* ---------- questions ---------- */

.q-excerpt {
  margin: 8px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: .92rem;
  color: var(--text);
}
.q-path { font-size: .78rem; color: var(--muted); margin-top: 8px; overflow-wrap: anywhere; }
.q-hint { font-size: .88rem; color: var(--muted); margin: 0 0 8px; }

.pics { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin-top: 10px; }
.pics a { display: block; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); background: var(--surface-2); }
.pics img { display: block; width: 100%; height: 120px; object-fit: cover; }

.answer { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; }
.answer textarea { min-height: 80px; }
.answer .actions { margin-top: 4px; }

.options { border: 0; padding: 0; margin: 0 0 6px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.options legend { padding: 0; margin-bottom: 4px; }
.option {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: .92rem;
  overflow-wrap: anywhere;
}
.option input { margin-top: 3px; flex: none; width: 18px; height: 18px; accent-color: var(--accent); }
.option:has(input:checked) { border-color: var(--accent); background: var(--busy-bg); }

.quote {
  margin: 8px 0 0;
  padding: 8px 10px;
  border-left: 3px solid var(--accent);
  background: var(--surface-2);
  border-radius: 6px;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 40vh;
  overflow: auto;
}

/* ---------- ask ---------- */

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form-row label, .field label { display: block; font-size: .82rem; color: var(--muted); margin-bottom: 4px; }
.field { margin-top: 10px; }

.chat-item { display: block; width: 100%; text-align: left; }
.chat-item .title { font-weight: 600; overflow-wrap: anywhere; }

.thread { display: flex; flex-direction: column; gap: 10px; }
.msg {
  max-width: 92%;
  border-radius: 14px;
  padding: 10px 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: .95rem;
}
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant pre {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 8px;
  overflow-x: auto;
  white-space: pre;
  font-size: .85rem;
  margin: 6px 0;
}
.msg.assistant code { font-family: ui-monospace, Consolas, monospace; font-size: .88em; }
.msg-state { align-self: flex-end; font-size: .8rem; color: var(--muted); }
.msg-state.error { color: var(--bad); }

.back { display: inline-flex; gap: 6px; align-items: center; }

/* ---------- log ---------- */

.log-item { display: flex; gap: 10px; font-size: .9rem; padding: 10px 0; border-bottom: 1px solid var(--border); }
.log-item:last-child { border-bottom: 0; }
.log-time { color: var(--muted); flex: none; width: 4.6em; font-variant-numeric: tabular-nums; }
.log-text { overflow-wrap: anywhere; min-width: 0; }
.log-text .wf { font-weight: 600; }
.log-item.alert-kind .log-text { color: var(--bad); }

/* ---------- dialog & toast ---------- */

dialog {
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  width: min(420px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { padding: 20px; }
dialog h2 { margin: 0 0 8px; font-size: 1.1rem; }
#dialog-body { color: var(--muted); font-size: .95rem; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 72px);
  z-index: 20;
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: .92rem;
  max-width: calc(100vw - 32px);
}

/* ---------- replies and updates ---------- */

.alert.reply { background: var(--ok-bg); color: var(--ok); align-items: center; }
.alert.reply strong { font-weight: 700; }

.chat-item.unread { border-color: var(--accent); }
.chat-item .card-head { align-items: center; }
.reply-preview {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  color: var(--text);
  font-size: .9rem;
  white-space: pre-line;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.reply-preview.waiting { color: var(--muted); font-style: italic; }
