:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #6b7684;
  --line: #e1e6ec;
  --primary: #2a7de1;
  --primary-dark: #1f63b4;
  --danger: #d64545;
  --ok: #2e9e5b;
  --warn: #d9932c;
  --radius: 12px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; margin: 0 0 8px; }
h2 { font-size: 18px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 12px 0 0; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
p { margin: 0 0 10px; }
code { background: #eef1f5; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
code.pw { font-size: 16px; padding: 3px 8px; user-select: all; }
kbd { border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font-size: 12px; background: #fff; }

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 24px; gap: 16px;
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 20px; }
.nav { display: flex; align-items: center; gap: 16px; }
.nav a { color: var(--text); padding: 4px 2px; border-bottom: 2px solid transparent; }
.nav a.active { border-color: var(--primary); }
.nav a:hover { text-decoration: none; color: var(--primary); }
.nav-user { color: var(--muted); }
.inline { display: inline; }

.container { max-width: 1100px; margin: 24px auto; padding: 0 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }
.crumbs { color: var(--muted); margin-bottom: 12px; }
.notes { white-space: pre-wrap; margin-top: 8px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 12px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters input, .filters select { padding: 7px 10px; }

/* buttons */
.btn {
  display: inline-block; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; color: var(--text); cursor: pointer; font: inherit; line-height: 1.2;
}
.btn:hover { border-color: #c5ccd5; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { color: var(--danger); border-color: #f0c9c9; }
.btn-danger:hover { background: #fdf2f2; }
.btn-link { border: none; background: none; color: var(--primary); padding: 4px 2px; }
.btn-block { width: 100%; }
.btn-sm { padding: 4px 9px; font-size: 13px; }
.btn[disabled] { opacity: .5; cursor: default; }
.btn-x { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 0 4px; }
.btn-x:hover { color: var(--danger); }

/* forms */
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form input, .form select, .form textarea {
  font: inherit; color: var(--text); padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
.form input:focus, .form select:focus, .form textarea:focus { outline: 2px solid #bcd6f7; border-color: var(--primary); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.form-grid .span-2 { grid-column: span 2; }
.form .actions { display: flex; gap: 8px; align-items: center; flex-direction: row; }
.form .check { flex-direction: row; align-items: center; }
.hint { font-size: 12px; color: var(--muted); }
.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; border: 1px solid; }
.alert-error { background: #fdf2f2; border-color: #f0c9c9; color: #a33; }
.alert-ok { background: #eefaf2; border-color: #bfe6cd; color: #216b3d; }
.alert-warn { background: #fff7e8; border-color: #f2ddb0; color: #7a5410; }
.auth-card { max-width: 380px; margin: 60px auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px; }
.auth-card .form { display: flex; flex-direction: column; gap: 12px; }

/* members */
.member-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.member-card {
  display: flex; gap: 14px; align-items: center; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; color: var(--text); transition: box-shadow .15s, transform .15s;
}
.member-card:hover { text-decoration: none; box-shadow: 0 6px 20px rgba(20,40,80,.08); transform: translateY(-1px); }
.member-name { font-weight: 600; font-size: 16px; }
.avatar { border-radius: 50%; background: #dfe8f5; color: var(--primary-dark); display: grid; place-items: center; font-weight: 700; overflow: hidden; flex-shrink: 0; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-lg { width: 64px; height: 64px; font-size: 22px; }
.avatar-xl { width: 110px; height: 110px; font-size: 36px; }
.member-head { display: flex; gap: 20px; align-items: center; }
.member-head-info { flex: 1; }
.member-head-actions { display: flex; gap: 8px; align-items: flex-start; }
.photo-preview img { max-height: 160px; border-radius: 8px; border: 1px solid var(--line); }

/* upload */
.dropzone {
  border: 2px dashed #c9d3e0; border-radius: var(--radius); padding: 28px 16px; text-align: center; cursor: pointer;
  background: #fafbfd; transition: background .15s, border-color .15s;
}
.dropzone:hover, .dropzone:focus { border-color: var(--primary); outline: none; }
.dropzone.drag { background: #e9f2fd; border-color: var(--primary); }
.dropzone-compact { padding: 14px; }
.dz-text { margin-bottom: 6px; }
.file-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.file-list:empty { display: none; }
.file-list li {
  display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px 4px 4px; font-size: 13px; background: #fff;
}
.file-list li img, .file-list li .ph { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; background: #eef1f5; display: grid; place-items: center; font-size: 11px; font-weight: 700; color: var(--muted); }
.file-list li.err { border-color: #f0c9c9; color: var(--danger); }
.file-list li .prog { height: 3px; background: var(--primary); width: 0; border-radius: 2px; }
.upload-card { border-color: #bcd6f7; }
body.page-drag .dropzone { background: #e9f2fd; border-color: var(--primary); }

/* analyses */
.analysis-list { display: flex; flex-direction: column; }
.analysis-row {
  display: grid; grid-template-columns: 100px 1fr auto; gap: 14px; align-items: center;
  padding: 12px 8px; border-top: 1px solid var(--line); color: var(--text);
}
.analysis-row:first-child { border-top: none; }
.analysis-row:hover { background: #f7f9fc; text-decoration: none; }
.a-date { font-variant-numeric: tabular-nums; color: var(--muted); }
.a-title { font-weight: 600; }
.a-thumbs { display: flex; gap: 4px; align-items: center; }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); background: #eef1f5; display: grid; place-items: center; font-size: 11px; font-weight: 700; color: var(--muted); }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-top: 12px; }
.gallery-item { margin: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; }
.gallery-item img, .pdf-tile { display: block; width: 100%; height: 160px; object-fit: cover; background: #eef1f5; }
.pdf-tile { display: grid; place-items: center; font-weight: 700; color: var(--danger); font-size: 22px; }
.pdf-tile:hover { text-decoration: none; }
.gallery-item figcaption { padding: 6px 8px; font-size: 13px; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; align-items: center; }
.gallery-item .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.factions { display: flex; gap: 6px; align-items: center; grid-column: 2; grid-row: 1 / span 2; }
.pdf-frame { width: 100%; height: 75vh; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; }
details summary { cursor: pointer; padding: 6px 0; }

/* admin */
.admin-layout { display: grid; grid-template-columns: 200px 1fr; gap: 20px; }
.admin-side { display: flex; flex-direction: column; gap: 4px; }
.admin-side a { padding: 8px 12px; border-radius: 8px; color: var(--text); }
.admin-side a.active { background: var(--card); border: 1px solid var(--line); font-weight: 600; }
.admin-side a:hover { text-decoration: none; background: #e9eef5; }
.admin-main { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; min-width: 0; }
.table { width: 100%; border-collapse: collapse; margin-bottom: 24px; font-size: 14px; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 500; font-size: 13px; }
.table input, .table select { font: inherit; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; }
.row-off { opacity: .55; }
.row-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.tag { display: inline-block; font-size: 11px; padding: 2px 7px; border-radius: 10px; background: #eef1f5; color: var(--muted); }
.tag-ok { background: #eefaf2; color: var(--ok); }
.tag-off { background: #f4f4f4; color: #777; }
.tag-warn { background: #fff7e8; color: var(--warn); }
.table-audit td { font-size: 13px; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; }

/* toast */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #1c2430; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 100;
  box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast.err { background: var(--danger); }

@media (max-width: 720px) {
  .topbar { padding: 8px 12px; flex-wrap: wrap; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: span 1; }
  .member-head { flex-direction: column; align-items: flex-start; }
  .analysis-row { grid-template-columns: 1fr; gap: 6px; }
  .admin-layout { grid-template-columns: 1fr; }
  .admin-side { flex-direction: row; flex-wrap: wrap; }
  .table, .table thead, .table tbody, .table tr, .table td { display: block; }
  .table th { display: none; }
  .table td { border: none; padding: 3px 0; }
  .table tr { border-bottom: 1px solid var(--line); padding: 8px 0; }
}

/* timeline */
.timeline { display: flex; flex-direction: column; gap: 6px; }
.tl-day { display: grid; grid-template-columns: 170px 1fr; gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); }
.tl-day:first-child { border-top: none; }
.tl-date { display: flex; flex-direction: column; gap: 2px; padding-top: 10px; position: relative; padding-left: 18px; }
.tl-dot { position: absolute; left: 0; top: 15px; width: 10px; height: 10px; border-radius: 50%; background: var(--primary); }
.tl-items .analysis-row { grid-template-columns: 1fr auto; border-top: none; padding: 8px; border-radius: 8px; }

/* tables */
.table-wrap { overflow-x: auto; }
.table-compact th, .table-compact td { padding: 6px 8px; font-size: 13.5px; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table th.num { text-align: right; }
.row-group td { background: #f4f6f9; font-weight: 600; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.row-flag td { background: #fff9f2; }
.row-current td { background: #eefaf2; }
.cell-flag { font-weight: 600; }
.cell-H, .cell-L { color: #b5541c; }
.cell-A { color: #a33; }
.arrow { font-size: 11px; margin-left: 2px; }
.pivot .sticky { position: sticky; left: 0; background: var(--card); z-index: 1; min-width: 200px; }
.flag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: #eef1f5; color: var(--muted); white-space: nowrap; }
.flag-N { background: #eefaf2; color: var(--ok); }
.flag-H, .flag-L { background: #fff4e5; color: #b5541c; }
.flag-A { background: #fdf2f2; color: var(--danger); }
.inline-form { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 6px 0; }
.inline-form input, .inline-form select { padding: 5px 8px; }
.w-90 { width: 90px; } .w-110 { width: 110px; } .w-140 { width: 140px; } .w-180 { width: 180px; }
.cat-title { margin-top: 20px; font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.explain p { margin: 6px 0; }
.extract-panel { border: 1px solid #bcd6f7; background: #f5f9ff; border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; }
.extract-panel select { max-width: 260px; padding: 4px 6px; font: inherit; }
.extract-panel input { padding: 4px 6px; font: inherit; border: 1px solid var(--line); border-radius: 6px; }
.extract-panel tr.unmatched td { background: #fff7e8; }

/* chart */
.chart-wrap { position: relative; margin: 8px 0 16px; }
.chart { width: 100%; height: auto; display: block; font-family: inherit; }
.chart .ref-band { fill: #2a7de1; fill-opacity: .07; }
.chart .ref-label { font-size: 11px; fill: var(--muted); }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { font-size: 11px; fill: var(--muted); }
.chart .line { fill: none; stroke: var(--primary); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .pt { fill: var(--card); stroke: var(--primary); stroke-width: 2; }
.chart .pt-out { fill: #fff4e5; stroke: #b5541c; }
.chart .pt-mark { font-size: 12px; font-weight: 700; fill: #b5541c; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .ptg:hover .pt { r: 7; }
.chart .val-label { font-size: 12px; fill: var(--text); font-weight: 600; }
.chart-tip { position: absolute; pointer-events: none; background: #1c2430; color: #fff; font-size: 12px; padding: 5px 8px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, -130%); }

@media (max-width: 720px) {
  .tl-day { grid-template-columns: 1fr; }
  .tl-date { flex-direction: row; gap: 8px; align-items: baseline; padding-top: 0; }
  .tl-dot { top: 5px; }
}

/* dialogs */
.dlg { border: none; border-radius: 14px; padding: 0; width: min(880px, calc(100vw - 32px)); max-height: calc(100vh - 40px); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.dlg::backdrop { background: rgba(20, 30, 50, .45); }
.dlg > div, .dlg > form { padding: 18px 22px 22px; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 22px 0 !important; }
.dlg-head h2 { margin: 0; }
.dlg .form-grid { margin-top: 8px; }

/* treatment form rows */
.tx-rows { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.tx-row { display: grid; grid-template-columns: 1.4fr 1fr 1fr 1.4fr 1fr 1fr .7fr auto; gap: 8px; align-items: end; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fafbfd; }
.tx-row .tx-remove { align-self: center; }
.tx-common { margin-top: 12px; }
.tx-row input, .tx-row select { min-width: 0; width: 100%; }

/* treatment chips + lane */
.tx-chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px 3px 7px; border-radius: 12px; background: #eef1f5; color: var(--text); font-size: 12.5px; line-height: 1.3; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; text-decoration: none; }
.tx-chip:hover { text-decoration: none; filter: brightness(.97); }
.tx-chip i { width: 8px; height: 8px; border-radius: 50%; background: var(--c, #6b7684); flex-shrink: 0; }
.tx-chip.tx-start { font-weight: 600; }
.tx-chip.tx-end { opacity: .8; }
.tx-c0 { --c: #2a7de1; background: #e6f0fc; } .tx-c1 { --c: #d9932c; background: #fdf1de; } .tx-c2 { --c: #2e9e5b; background: #e4f5ea; }
.tx-c3 { --c: #8b5cf6; background: #efe9fd; } .tx-c4 { --c: #d64545; background: #fbe6e6; } .tx-c5 { --c: #0f8f9e; background: #dff4f6; }
.tx-c6 { --c: #b45f8c; background: #f7e6ef; } .tx-c7 { --c: #6b7684; background: #eceff3; }
.current-tx { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 8px; }

.timeline.has-tx .tl-day, .timeline.has-tx .tl-head { grid-template-columns: 220px 150px 1fr; }
.tl-head { display: grid; gap: 12px; padding: 0 0 6px; }
.tl-lane-head { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.timeline.has-tx { gap: 0; }
.tl-lane { display: flex; gap: 8px; align-items: stretch; margin: -9px 0 -8px; border-right: 1px solid var(--line); padding-right: 8px; min-height: 100%; }
.tx-tracks { display: flex; gap: 4px; align-items: stretch; }
.tx-track { width: 12px; display: flex; justify-content: center; align-items: stretch; }
.tx-bar { display: block; width: 6px; background: var(--c); opacity: .85; min-height: 100%; }
.tx-bar:hover { opacity: 1; width: 8px; }
.tx-bar.is-start { border-radius: 0 0 4px 4px; margin-top: 0; }
.tx-bar.is-end { border-radius: 4px 4px 0 0; }
.tx-bar.is-start.is-end, .tx-bar.is-once { border-radius: 4px; min-height: 0; height: 22px; align-self: center; width: 10px; }
.tx-labels { display: flex; flex-direction: column; gap: 4px; justify-content: flex-start; padding: 18px 0 10px; min-width: 0; }
.tx-labels .tx-chip { max-width: 100%; }
.tl-empty { padding: 12px 8px; }

@media (max-width: 900px) {
  .timeline.has-tx .tl-day, .timeline.has-tx .tl-head { grid-template-columns: 1fr; }
  .tl-lane { border-right: none; margin: 0; }
  .tx-labels { flex-direction: row; flex-wrap: wrap; padding: 0; }
  .tl-head { display: none; }
  .tx-row { grid-template-columns: 1fr 1fr; }
}

/* AI panel */
.ai-card { border-color: #d9c8f5; }
.tabs { display: flex; gap: 4px; background: #eef1f5; padding: 3px; border-radius: 10px; }
.tab { border: none; background: transparent; padding: 6px 14px; border-radius: 8px; cursor: pointer; font: inherit; color: var(--muted); }
.tab.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.ai-messages { display: flex; flex-direction: column; gap: 12px; max-height: 70vh; overflow-y: auto; padding: 4px 2px; }
.ai-msg { border-radius: 12px; padding: 10px 14px; max-width: 100%; }
.ai-user { background: #eef3fb; align-self: flex-end; max-width: 85%; }
.ai-assistant { background: #f7f5fc; border: 1px solid #e6dff5; }
.ai-meta { font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.ai-body h3, .ai-body h4, .ai-body h5 { margin: 12px 0 6px; font-size: 15px; }
.ai-body h3:first-child { margin-top: 0; }
.ai-body p { margin: 6px 0; }
.ai-body ul, .ai-body ol { margin: 6px 0 6px 20px; padding: 0; }
.ai-body li { margin: 3px 0; }
.ai-body .md-table { margin: 8px 0; font-size: 13px; }
.ai-body pre { background: #eef1f5; padding: 8px 10px; border-radius: 8px; overflow-x: auto; }
.ai-form { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.ai-form textarea { font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; resize: vertical; }
.ai-form textarea:focus { outline: 2px solid #bcd6f7; border-color: var(--primary); }
.ai-thinking { display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: 13px; padding: 8px 0; }
.ai-thinking i { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.ai-history { margin-top: 12px; border-top: 1px dashed var(--line); padding-top: 10px; }
.ai-history details { margin: 6px 0; }
.ai-history summary { cursor: pointer; color: var(--primary); }
.check.inline { display: inline-flex; gap: 4px; align-items: center; margin-left: 6px; }

.ai-row { background: #faf8ff; border-radius: 8px; }
.ai-row:hover { background: #f3effc; }
.thumb-ai { background: #efe9fd; color: #6d4fc4; }
.ai-msg.ai-flash { outline: 2px solid #8b5cf6; }
