:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --ink: #17212e;
  --muted: #5d6b7c;
  --line: #dde3ea;
  --brand: #1f3a5f;
  --brand-ink: #ffffff;
  --accent: #2563eb;
  --red: #d64534;   --red-bg: #fde4e1;
  --yellow: #b7860b; --yellow-bg: #fdf1c7;
  --green: #1e8f3e; --green-bg: #d9f7d0;
  --gt: #0f766e;
  --il: #7c3aed;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: 0 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* --- top bar --- */
.topbar { background: var(--brand); color: var(--brand-ink); }
.topbar .inner { max-width: 1280px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 1.5rem; min-height: 56px; flex-wrap: wrap; }
.brand { font-weight: 700; letter-spacing: .02em; color: #fff; font-size: 1.05rem; }
.brand:hover { text-decoration: none; }
.brand span { opacity: .65; font-weight: 500; }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.nav a { color: #dbe6f3; padding: .45rem .75rem; border-radius: 8px; }
.nav a:hover, .nav a.on { background: rgba(255,255,255,.12); color: #fff; text-decoration: none; }
.nav .right { margin-left: auto; }
.topbar .spacer { flex: 1; }

main { max-width: 1280px; margin: 0 auto; padding: 20px 16px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }

/* --- surfaces --- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card, .stack > * + * { margin-top: 16px; }
.grid > .card { margin-top: 0; }
details.card > summary { cursor: pointer; }
details.card[open] > summary { margin-bottom: .75rem; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.lines { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }

.flash { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
.flash.ok { background: var(--green-bg); color: #135c28; }
.flash.err { background: var(--red-bg); color: #8c2317; }
.flash.warn { background: var(--yellow-bg); color: #6d5006; }

/* --- buttons & inputs --- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; border: 1px solid var(--line); background: var(--surface); color: var(--ink); padding: .55rem 1rem; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer; min-height: 42px; }
.btn:hover { text-decoration: none; border-color: #c3ccd7; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1d4fd0; }
.btn.danger { color: var(--red); }
.btn.big { min-height: 52px; padding: .75rem 1.5rem; font-size: 1.05rem; }
.btn.small { min-height: 32px; padding: .25rem .6rem; font-size: .85rem; }
.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

label { display: block; font-weight: 600; font-size: .85rem; margin-bottom: .3rem; color: #334155; }
input, select, textarea { width: 100%; font: inherit; padding: .55rem .7rem; border: 1px solid #c9d2dd; border-radius: 8px; background: #fff; color: var(--ink); min-height: 44px; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(37, 99, 235, .35); border-color: var(--accent); }
input[readonly] { background: #f1f4f8; color: var(--muted); }
.field { min-width: 0; }
.hint { font-size: .8rem; color: var(--muted); margin-top: .25rem; }
.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }

.range { display: flex; gap: .5rem; align-items: flex-end; flex-wrap: wrap; }
.range .field { width: 160px; }
.presets { display: flex; gap: .35rem; flex-wrap: wrap; margin-top: .5rem; }
.presets a { font-size: .85rem; padding: .25rem .6rem; border-radius: 999px; background: #e8eef6; color: var(--brand); }
.presets a.on { background: var(--brand); color: #fff; }
.presets a:hover { text-decoration: none; }

/* --- OEE pill & colours --- */
.pill { display: inline-block; padding: .15rem .55rem; border-radius: 999px; font-weight: 700; font-variant-numeric: tabular-nums; }
.b-red { background: var(--red-bg); color: var(--red); }
.b-yellow { background: var(--yellow-bg); color: var(--yellow); }
.b-green { background: var(--green-bg); color: var(--green); }
.b-over { background: #f3e8ff; color: #7c2d12; outline: 2px dashed var(--red); outline-offset: -2px; }
.b-none { background: #eef1f5; color: var(--muted); }
.legend { display: flex; gap: .75rem; flex-wrap: wrap; font-size: .8rem; color: var(--muted); }
.legend span::before { content: ""; display: inline-block; width: .7rem; height: .7rem; border-radius: 3px; margin-right: .3rem; vertical-align: -1px; }
.legend .l-red::before { background: var(--red); }
.legend .l-yellow::before { background: #e3b30f; }
.legend .l-green::before { background: var(--green); }
.legend .l-over::before { background: #f3e8ff; outline: 2px dashed var(--red); outline-offset: -2px; }

/* --- KPI tiles --- */
.kpi .label { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.kpi .value { font-size: 1.9rem; font-weight: 750; font-variant-numeric: tabular-nums; margin-top: .15rem; }
.kpi .sub { font-size: .85rem; color: var(--muted); }

/* --- home line tiles --- */
.group-title { display: flex; align-items: center; gap: .5rem; margin: 1.25rem 0 .6rem; font-size: 1rem; }
.group-title .tag { font-size: .75rem; padding: .1rem .5rem; border-radius: 6px; color: #fff; }
.tag.gt { background: var(--gt); }
.tag.il { background: var(--il); }
.line-tile { display: flex; flex-direction: column; gap: .6rem; color: var(--ink); border-left: 5px solid var(--line); }
.line-tile:hover { text-decoration: none; border-color: var(--accent); }
.line-tile.t-red { border-left-color: var(--red); }
.line-tile.t-yellow { border-left-color: #e3b30f; }
.line-tile.t-green { border-left-color: var(--green); }
.line-tile.t-over { border-left-color: var(--red); border-left-style: dashed; }
.line-tile .name { font-weight: 700; font-size: 1.05rem; }
.line-tile .stats { display: flex; flex-wrap: wrap; gap: .15rem .75rem; font-size: .85rem; color: var(--muted); }
.line-tile .oee { font-size: 1.6rem; font-weight: 750; font-variant-numeric: tabular-nums; }

/* --- tables --- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .75rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 650; background: #f8fafc; position: sticky; top: 0; }
tr.highlight td { background: #eef5ff; }
td .stops { font-size: .8rem; color: var(--muted); margin-top: .2rem; }
td.warn { color: var(--red); font-size: .8rem; }
tfoot td { font-weight: 700; background: #f8fafc; }

/* --- bars --- */
.bars { display: grid; gap: .45rem; }
.bar { display: grid; grid-template-columns: minmax(120px, 38%) 1fr auto; gap: .6rem; align-items: center; font-size: .85rem; }
.bar .track { background: #edf1f6; border-radius: 4px; height: 12px; overflow: hidden; }
.bar .fill { background: var(--brand); height: 100%; border-radius: 4px; }
.bar .fill.alt { background: #e0773a; }
.bar .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .lbl small { color: var(--muted); }

.trend { display: flex; align-items: flex-end; gap: 4px; height: 150px; padding-top: 1rem; border-bottom: 1px solid var(--line); }
.trend .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; min-width: 14px; position: relative; }
.trend .col .v { width: 100%; max-width: 38px; border-radius: 4px 4px 0 0; }
.trend .col .t { position: absolute; top: -2px; font-size: .7rem; color: var(--muted); }
.trend-x { display: flex; gap: 4px; font-size: .7rem; color: var(--muted); }
.trend-x span { flex: 1; text-align: center; min-width: 14px; overflow: hidden; }
.v.red { background: var(--red); } .v.yellow { background: #e3b30f; } .v.green { background: var(--green); } .v.none { background: #cbd5e1; } .v.over { background: repeating-linear-gradient(45deg, var(--red) 0 6px, #f3a79d 6px 12px); }

/* --- entry form --- */
.live { position: sticky; top: 8px; }
.live .row { display: flex; justify-content: space-between; padding: .45rem 0; border-bottom: 1px dashed var(--line); }
.live .row:last-child { border-bottom: 0; }
.live .big { font-size: 2.2rem; font-weight: 800; font-variant-numeric: tabular-nums; text-align: center; padding: .4rem 0; border-radius: 8px; margin-bottom: .5rem; }
.form-actions { grid-column: 1; }
.form-layout { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr) 300px; align-items: start; }
@media (max-width: 900px) { .form-layout { grid-template-columns: 1fr; } .live { position: static; } }
.stop-row { display: grid; grid-template-columns: 1fr 1.4fr 110px 44px; gap: .5rem; align-items: end; padding: .5rem 0; border-bottom: 1px solid var(--line); }
@media (max-width: 640px) { .stop-row { grid-template-columns: 1fr 1fr; } .stop-row .del { justify-self: end; } }
.stop-row label { font-size: .75rem; }
.product-box { position: relative; }
.suggest { position: absolute; z-index: 10; left: 0; right: 0; top: 100%; background: #fff; border: 1px solid #c9d2dd; border-radius: 8px; box-shadow: 0 8px 24px rgba(16,24,40,.14); max-height: 320px; overflow: auto; }
.suggest button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: .55rem .7rem; font: inherit; cursor: pointer; border-bottom: 1px solid var(--line); }
.suggest button:hover, .suggest button.on { background: #eef5ff; }
.suggest b { font-variant-numeric: tabular-nums; }
.product-info { font-size: .85rem; margin-top: .35rem; min-height: 1.2em; }
.product-info.bad { color: var(--red); }

.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs a { padding: .6rem 1rem; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 600; }
.tabs a.on { color: var(--ink); border-bottom-color: var(--accent); }
.tabs a:hover { text-decoration: none; color: var(--ink); }

.login { max-width: 360px; margin: 10vh auto; }
.empty { text-align: center; padding: 3rem 1rem; }
code { background: #eef1f5; padding: .1rem .35rem; border-radius: 4px; font-size: .9em; }

@media print {
  .topbar, .no-print, .presets, .range { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; }
}

/* --- analysis page --- */
.chart { position: relative; width: 100%; margin-top: .75rem; }
.nowrap { white-space: nowrap; }
.tests { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.test { border: 1px solid var(--line); border-left: 4px solid #cbd5e1; border-radius: 8px; padding: 12px 14px; }
.test.sig { border-left-color: var(--accent); }
.test.nosig { border-left-color: #94a3b8; }
.test-head { display: flex; flex-direction: column; gap: .3rem; }
.test h3 { margin: 0; }
.test .badge { align-self: flex-start; font-size: .72rem; font-weight: 700; padding: .1rem .5rem; border-radius: 999px; background: #eef1f5; color: var(--muted); }
.test.sig .badge { background: #e0ecff; color: #1d4fd0; }
.test .q { color: var(--muted); font-size: .85rem; margin: .4rem 0; }
.test .v { margin: .4rem 0; }
.test .meta { font-size: .78rem; color: var(--muted); margin: .2rem 0 0; }
.test details { margin-top: .5rem; }
.test table th, .test table td { padding: .3rem .4rem; }
.ai-card { border-left: 4px solid var(--brand); }
.ai-out { margin-top: 1rem; padding: 1rem 1.1rem; background: #f6f8fb; border-radius: 8px; line-height: 1.6; }
.ai-out h3 { margin: .75rem 0 .25rem; font-size: .95rem; }
.ai-out h3:first-child { margin-top: 0; }
.ai-out p { margin: .35rem 0; }
.ai-out ul { margin: .25rem 0; padding-left: 1.2rem; }
.ai-out.loading { color: var(--muted); font-style: italic; }
.ai-out.error { background: var(--red-bg); color: #8c2317; }
table.heat { font-size: .78rem; }
table.heat td, table.heat th { padding: .35rem .4rem; text-align: center; min-width: 38px; border: 2px solid #fff; }
table.heat td:first-child, table.heat th:first-child { text-align: left; }
table.heat td.b-red, table.heat td.b-yellow, table.heat td.b-green, table.heat td.b-over { border-radius: 4px; font-weight: 600; }

/* --- chat assistant --- */
.chat-fab { position: fixed; right: 20px; bottom: 20px; z-index: 50; display: inline-flex; align-items: center; gap: .5rem;
  background: var(--brand); color: #fff; border: 0; border-radius: 999px; padding: .75rem 1.1rem; font: inherit; font-weight: 650;
  box-shadow: 0 6px 20px rgba(16,24,40,.25); cursor: pointer; }
.chat-fab:hover { background: #274a78; }
.chat-fab.on { background: #0f2640; }
.chat-panel { position: fixed; right: 20px; bottom: 84px; z-index: 50; width: 420px; max-width: calc(100vw - 32px);
  height: min(640px, calc(100vh - 110px)); display: flex; flex-direction: column; background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 16px 48px rgba(16,24,40,.22); overflow: hidden; }
.chat-panel[hidden] { display: none; }
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: .75rem 1rem; background: var(--brand); color: #fff; }
.chat-sub { font-size: .75rem; opacity: .75; }
.chat-head-btns { display: flex; gap: .25rem; }
.chat-icon { background: transparent; border: 0; color: #fff; font-size: 1.05rem; width: 34px; height: 34px; border-radius: 8px; cursor: pointer; }
.chat-icon:hover { background: rgba(255,255,255,.14); }
.chat-log { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .75rem; background: #f6f8fb; }
.chat-msg { max-width: 92%; padding: .6rem .8rem; border-radius: 12px; font-size: .9rem; line-height: 1.5; }
.chat-msg p { margin: .15rem 0; }
.chat-msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.chat-msg.bot { align-self: flex-start; background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; width: 92%; }
.chat-msg.bot.error { background: var(--red-bg); color: #8c2317; }
.chat-msg.wait { color: var(--muted); font-style: italic; width: auto; }
.chat-li { margin: .1rem 0 .1rem .25rem; }
.chat-chips { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .6rem; }
.chat-chip { font: inherit; font-size: .8rem; border: 1px solid #c9d6ea; background: #eef4fd; color: var(--brand); border-radius: 999px; padding: .3rem .65rem; cursor: pointer; text-align: left; }
.chat-chip:hover { background: #e0ecff; }
.chat-link { margin-top: .5rem; margin-right: .35rem; }
.chat-q { margin-top: .5rem; border-top: 1px dashed var(--line); padding-top: .4rem; font-size: .8rem; }
.chat-q summary { cursor: pointer; color: var(--muted); }
.chat-sql { background: #0f172a; color: #e2e8f0; padding: .5rem .6rem; border-radius: 6px; font-size: .72rem; white-space: pre-wrap; word-break: break-word; margin: .4rem 0; }
.chat-table { overflow-x: auto; max-height: 240px; overflow-y: auto; border: 1px solid var(--line); border-radius: 6px; }
.chat-table table { font-size: .75rem; }
.chat-table th, .chat-table td { padding: .3rem .45rem; white-space: nowrap; }
.chat-err { color: var(--red); margin-top: .25rem; }
.chat-warn { margin-top: .5rem; font-size: .8rem; background: var(--yellow-bg); color: #6d5006; padding: .35rem .5rem; border-radius: 6px; }
.chat-empty { color: var(--muted); margin-top: .25rem; }
.chat-form { display: flex; gap: .5rem; padding: .6rem; border-top: 1px solid var(--line); background: #fff; align-items: flex-end; }
.chat-form textarea { resize: none; min-height: 42px; max-height: 120px; line-height: 1.4; }
.chat-form .btn { min-width: 46px; }
.chat-foot { font-size: .7rem; color: var(--muted); padding: 0 .75rem .6rem; background: #fff; }
.chat-panel.wide { width: 760px; height: min(820px, calc(100vh - 110px)); }
.chat-msg h4 { margin: .5rem 0 .15rem; font-size: .85rem; }
.chat-steps { margin: .35rem 0 0; padding-left: 1.2rem; font-size: .78rem; color: var(--muted); font-style: normal; }
.chat-chart { margin: .6rem 0 .2rem; }
.chat-chart figcaption { font-size: .78rem; font-weight: 650; color: var(--ink); margin-bottom: .25rem; }
.chat-chart-box { position: relative; height: 240px; }
.chat-file { background: #e8f5ec; border-color: #b7e0c3; color: #135c28; }
.chat-export { display: flex; align-items: center; gap: .35rem; flex-wrap: wrap; margin-top: .6rem; padding-top: .5rem;
  border-top: 1px solid var(--line); font-size: .78rem; }
.chat-dl { font: inherit; font-size: .78rem; border: 1px solid var(--line); background: #fff; border-radius: 6px;
  padding: .2rem .55rem; cursor: pointer; color: var(--brand); font-weight: 600; }
.chat-dl:hover { background: #eef4fd; }
@media (max-width: 640px) {
  .chat-panel.wide { width: 100vw; height: 100dvh; }
  .chat-panel { right: 0; bottom: 0; width: 100vw; max-width: 100vw; height: 100dvh; border-radius: 0; }
  .chat-fab { right: 14px; bottom: 14px; padding: .7rem; }
  .chat-fab span { display: none; }
}
body:has(.chat-fab) main { padding-bottom: 96px; }
