:root {
  --blue: #28639a;
  --blue-ink: #1d4a74;
  --blue-soft: #e8f0f8;
  --ink: #16202a;
  --muted: #5b6875;
  --bg: #eef1f4;
  --card: #ffffff;
  --line: #d9dfe5;
  --amber: #8a4b00;
  --amber-bg: #fff1d6;
  --red: #b42318;
  --red-bg: #fdecea;
  --green: #17693f;
  --green-bg: #e3f4ea;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --blue: #6aa6dc;
    --blue-ink: #9cc6ec;
    --blue-soft: #1c2a38;
    --ink: #e8edf2;
    --muted: #9aa7b4;
    --bg: #0f1419;
    --card: #182028;
    --line: #2a3541;
    --amber: #f4b860;
    --amber-bg: #3a2a12;
    --red: #ff8a80;
    --red-bg: #3b1f1d;
    --green: #7fd8a6;
    --green-bg: #173226;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-size: 17px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* header */
.top { position: sticky; top: 0; z-index: 10; }
.bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px 8px 16px; padding-top: calc(8px + env(safe-area-inset-top));
  background: #fff; border-bottom: 1px solid #d9dfe5;
}
.logo { height: 30px; width: auto; display: block; }
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 10px; background: transparent; color: #1d4a74;
  display: grid; place-items: center;
}
.icon-btn:active { background: #e8f0f8; }
.machine {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 16px; background: var(--blue-ink); color: #fff;
}
@media (prefers-color-scheme: dark) { .machine { background: #13324f; } }
.machine-text { display: flex; flex-direction: column; min-width: 0; }
.machine-text strong { font-size: 16px; }
.machine-text span { font-size: 13px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.engine {
  flex: none; font-size: 12px; font-weight: 600; padding: 4px 9px; border-radius: 999px;
  background: rgba(255, 255, 255, .16); border: 1px solid rgba(255, 255, 255, .35); white-space: nowrap;
}

/* chat */
main { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(120px + env(safe-area-inset-bottom)); }
.welcome h1 { font-size: 24px; margin: 8px 0 6px; }
.welcome p { color: var(--muted); margin: 0 0 16px; }
.chips { display: flex; flex-direction: column; gap: 8px; }
.chip {
  text-align: left; padding: 12px 14px; min-height: 48px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.chip:active { background: var(--blue-soft); }

.msg { margin: 14px 0; }
.msg.user {
  margin-left: auto; max-width: 85%; width: fit-content;
  background: var(--blue); color: #fff; padding: 10px 14px; border-radius: 16px 16px 4px 16px;
  overflow-wrap: anywhere;
}
@media (prefers-color-scheme: dark) { .msg.user { color: #0f1419; } }
.msg.bot {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.answer p { margin: 0 0 10px; }
.answer p.h { font-weight: 700; }
.answer ol, .answer ul { margin: 0 0 10px; padding-left: 22px; }
.answer li { margin: 4px 0; }
.cite {
  display: inline-grid; place-items: center; min-width: 24px; height: 24px; padding: 0 6px; margin: 0 2px;
  border-radius: 999px; border: 1px solid var(--blue); background: var(--blue-soft); color: var(--blue-ink);
  font-size: 13px; font-weight: 700; vertical-align: 2px; line-height: 1;
}
.note {
  margin: 0 0 10px; padding: 8px 12px; border-radius: 10px; background: var(--amber-bg); color: var(--amber);
  font-size: 15px;
}
.meta { margin-top: 10px; font-size: 13px; color: var(--muted); }
.pending { color: var(--muted); display: flex; align-items: center; gap: 10px; }
.dots span {
  display: inline-block; width: 7px; height: 7px; margin-right: 3px; border-radius: 50%;
  background: var(--blue); animation: blink 1.2s infinite both;
}
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* sources */
.sources { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.k { font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.source {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; width: 100%; text-align: left;
  padding: 10px 12px; margin: 6px 0 0; min-height: 44px; border-radius: 10px;
  border: 1px solid var(--line); background: transparent;
}
.source:active { background: var(--blue-soft); }
.source .n {
  display: inline-grid; place-items: center; min-width: 22px; height: 22px; border-radius: 999px;
  background: var(--blue); color: #fff; font-size: 12px; font-weight: 700;
}
@media (prefers-color-scheme: dark) { .source .n { color: #0f1419; } }
.source .lbl { font-weight: 600; }
.source .det { color: var(--muted); font-size: 14px; width: 100%; padding-left: 30px; }
.tag { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.tag.warn { background: var(--amber-bg); color: var(--amber); }
.tag.crit { background: var(--red-bg); color: var(--red); }
.tag.ok { background: var(--green-bg); color: var(--green); }
.tool-sum { margin: 0 0 8px; font-size: 15px; }
.table-wrap { overflow-x: auto; margin: 0 0 10px; }
.answer table { border-collapse: collapse; font-size: 15px; min-width: 60%; }
.answer th, .answer td { border-bottom: 1px solid var(--line); padding: 6px 10px 6px 0; text-align: left; vertical-align: top; }
.answer th { font-size: 13px; color: var(--muted); font-weight: 700; }
.passage { margin-top: 10px; }
.passage p { margin: 6px 0 0; font-size: 15px; color: var(--muted); }

/* alarm card */
.alarm-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.code {
  font-weight: 800; font-size: 18px; letter-spacing: .02em; padding: 3px 10px; border-radius: 8px;
  background: var(--red-bg); color: var(--red); border: 1px solid currentColor;
}
.loc { font-size: 13px; font-weight: 700; letter-spacing: .04em; color: var(--muted); }
.alarm-msg { font-size: 19px; line-height: 1.3; margin: 10px 0 12px; }
.alarm-sec { margin-top: 12px; }
.alarm-sec p { margin: 0; }
.alarm-sec ol { margin: 0; padding-left: 22px; }
.alarm-sec li { margin: 5px 0; }
.device { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-top: 8px; }
.device-tag { font-weight: 800; }
.part { margin-top: 6px; font-size: 15px; }
.part .pn { font-weight: 700; }
.part .sub { color: var(--muted); font-size: 14px; }
.pill-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.pill {
  min-height: 40px; padding: 8px 12px; border-radius: 999px; border: 1px solid var(--blue);
  background: var(--blue-soft); color: var(--blue-ink); font-weight: 600; font-size: 15px;
}
.video-btn { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* composer */
.composer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}
.composer-inner { max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; }
.code-btn {
  flex: none; width: 64px; min-height: 48px; border-radius: 12px; border: 1px solid var(--red);
  background: var(--red-bg); color: var(--red); font-size: 13px; font-weight: 800; line-height: 1.1;
}
#q {
  flex: 1; min-height: 48px; max-height: 132px; resize: none; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); font-size: 17px; line-height: 1.35;
}
#q:focus { outline: 2px solid var(--blue); outline-offset: 0; }
.send-btn {
  flex: none; width: 48px; height: 48px; border-radius: 12px; border: 0; background: var(--blue); color: #fff;
  display: grid; place-items: center;
}
@media (prefers-color-scheme: dark) { .send-btn { color: #0f1419; } }
.send-btn:disabled, .code-btn:disabled { opacity: .5; }

/* dialogs */
dialog { border: 0; padding: 0; color: var(--ink); background: var(--card); }
dialog::backdrop { background: rgba(10, 16, 22, .55); }
dialog.sheet {
  width: 100%; max-width: 900px; height: 100%; max-height: 100%; margin: 0 auto; border-radius: 0;
}
@media (min-width: 700px) { dialog.sheet { height: 92vh; margin-top: 4vh; border-radius: 16px; } }
.sheet-head {
  position: sticky; top: 0; z-index: 1; display: flex; align-items: center; gap: 10px; justify-content: space-between;
  padding: 10px 12px 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--line);
}
.sheet-title { font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
.close-btn {
  flex: none; min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg);
  font-weight: 600;
}
.sheet-body { padding: 14px 16px calc(24px + env(safe-area-inset-bottom)); }
.page-img { display: block; width: 100%; height: auto; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
@media (max-width: 700px) { .page-img.wide { width: 170%; max-width: none; } }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.hint { color: var(--muted); font-size: 14px; margin: 10px 0; }
.sheet-body video, .sheet-body iframe { width: 100%; aspect-ratio: 16 / 9; border: 0; border-radius: 8px; background: #000; }
.chunk-text { white-space: pre-wrap; font-size: 16px; }
a.link { color: var(--blue-ink); font-weight: 600; }

dialog.small { width: calc(100% - 32px); max-width: 380px; border-radius: 16px; padding: 18px; }
.code-input { display: flex; align-items: center; gap: 8px; margin: 8px 0 16px; }
.code-input span { font-size: 34px; font-weight: 800; color: var(--red); }
.code-input input {
  flex: 1; min-width: 0; font-size: 34px; font-weight: 800; letter-spacing: .1em; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg);
}
.row { display: flex; gap: 10px; justify-content: flex-end; }
.row button, button.primary { min-height: 48px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; }
button.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
@media (prefers-color-scheme: dark) { button.primary { color: #0f1419; } }

.opt {
  display: flex; gap: 12px; align-items: flex-start; padding: 12px; border: 1px solid var(--line); border-radius: 12px; margin: 8px 0;
}
.opt input { width: 22px; height: 22px; margin-top: 2px; flex: none; }
.opt small { display: block; color: var(--muted); font-size: 14px; }
.opt.disabled { opacity: .55; }
.facts { margin: 6px 0 0; padding-left: 20px; color: var(--muted); font-size: 15px; }

/* login */
body.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  padding: 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px;
}
.login-logo { width: 100%; height: auto; background: #fff; padding: 8px; border-radius: 8px; }
.login-card h1 { margin: 4px 0 0; font-size: 22px; }
.login-card p { margin: 0; color: var(--muted); }
.login-card input { min-height: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-size: 17px; }
.login-card .error { color: var(--red); }
