:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #f0f3ee;
  --text: #1c2620;
  --muted: #61705f;
  --line: #d9e0d6;
  --brand: #1f7a3d;
  --brand-dark: #145c2c;
  --brand-soft: #e3f1e6;
  --danger: #c0392b;
  --danger-soft: #fbeae8;
  --warn: #c9a227;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20, 40, 25, 0.08), 0 4px 12px rgba(20, 40, 25, 0.05);
  --score-1: #d1495b;
  --score-2: #e08a3c;
  --score-3: #d9c14a;
  --score-4: #7bbf6a;
  --score-5: #2f9e44;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a15;
    --surface: #1d251e;
    --surface-2: #232d24;
    --text: #e8efe6;
    --muted: #9db09a;
    --line: #33402f;
    --brand: #4caf6a;
    --brand-dark: #3d9556;
    --brand-soft: #22331f;
    --danger: #e0655a;
    --danger-soft: #3a1f1c;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text); line-height: 1.5;
  -webkit-text-size-adjust: 100%; overscroll-behavior-y: contain;
}
a { color: var(--brand-dark); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .2rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 0 0 .3rem; }
.muted { color: var(--muted); }
.boot, .loading { padding: 3rem 1rem; text-align: center; color: var(--muted); }

/* Layout ---------------------------------------------------------------- */
.shell { min-height: 100vh; }
.nav {
  display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1rem;
  padding: .6rem clamp(.8rem, 3vw, 2rem);
  padding-top: calc(.6rem + env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.nav-refresh { font-size: 1.15rem; line-height: 1; padding: .3rem .5rem; }

/* Pull-to-refresh (standalone / home-screen mode) ------------------------- */
.ptr {
  position: fixed; left: 0; right: 0; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: center; gap: .5rem;
  height: 110px; padding-top: env(safe-area-inset-top);
  background: var(--surface); border-bottom: 1px solid var(--line);
  color: var(--muted); font-size: .9rem;
  transform: translateY(-100%);
}
.ptr.ready .ptr-text { color: var(--brand-dark); font-weight: 600; }
.ptr-spinner {
  width: 1rem; height: 1rem; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--brand);
}
.ptr.spinning .ptr-spinner { animation: ptr-spin .7s linear infinite; }
@keyframes ptr-spin { to { transform: rotate(360deg); } }
.nav-brand a { font-weight: 700; color: var(--text); font-size: 1.05rem; }
.leaf { filter: saturate(1.3); }
.nav-links { display: flex; flex-wrap: wrap; gap: .25rem; }
.nav-user { margin-left: auto; display: flex; align-items: center; gap: .5rem; }
.nav-link {
  padding: .35rem .6rem; border-radius: 8px; color: var(--muted); font-size: .92rem;
}
.nav-link:hover { background: var(--surface-2); text-decoration: none; }
.nav-link[aria-current="page"] { background: var(--brand-soft); color: var(--brand-dark); font-weight: 600; }
.main { padding: clamp(.8rem, 3vw, 2rem); max-width: 1100px; margin: 0 auto; }
.view { display: flex; flex-direction: column; gap: 1rem; }

/* Cards --------------------------------------------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow);
}
.card.empty { text-align: center; color: var(--muted); padding: 2rem; }
.error-card { border-color: var(--danger); }
.section { display: flex; flex-direction: column; gap: .6rem; }
.page-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .8rem; align-items: flex-start; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; }
.head-actions { display: flex; flex-wrap: wrap; gap: .4rem; }
.crumbs { font-size: .85rem; color: var(--muted); }

/* Buttons -------------------------------------------------------------------- */
.btn {
  font: inherit; cursor: pointer; border: 1px solid var(--line);
  background: var(--surface); color: var(--text);
  padding: .5rem .85rem; border-radius: 9px; line-height: 1;
  display: inline-flex; align-items: center; gap: .4rem;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-sm { padding: .35rem .6rem; font-size: .85rem; }
.btn-block { width: 100%; justify-content: center; margin-top: .3rem; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(.94); }
.btn-danger-outline { color: var(--danger); border-color: var(--danger); }
.btn-warn-outline { color: var(--warn); border-color: var(--warn); }
.link-danger { border: 0; background: none; color: var(--danger); cursor: pointer; font-size: .8rem; padding: 0 .3rem; }

/* Auth ------------------------------------------------------------------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: .6rem; }
.auth-tabs { display: flex; gap: .3rem; background: var(--surface-2); padding: .25rem; border-radius: 10px; }
.auth-tab {
  flex: 1; font: inherit; cursor: pointer; padding: .45rem .5rem; border-radius: 8px;
  border: 1px solid transparent; background: transparent; color: var(--muted);
}
.auth-tab[aria-current="true"] { background: var(--surface); border-color: var(--line); color: var(--text); font-weight: 600; }

/* Forms ---------------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .5rem; }
.field-label { font-size: .85rem; font-weight: 600; color: var(--muted); }
.field-hint { font-size: .78rem; color: var(--muted); }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: .5rem .6rem; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: var(--brand); }
textarea { resize: vertical; }
.inline-field { display: inline-flex; align-items: center; gap: .4rem; font-size: .85rem; color: var(--muted); }
.inline-field select, .inline-field input { width: auto; }

/* Batch list --------------------------------------------------------------- */
.batch-grid { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.batch-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .9rem; box-shadow: var(--shadow); color: var(--text); display: block;
}
.batch-card:hover { text-decoration: none; border-color: var(--brand); }
.batch-card-top { display: flex; justify-content: space-between; gap: .5rem; align-items: start; }
.batch-card-meta { display: flex; flex-wrap: wrap; gap: .3rem .8rem; font-size: .8rem; color: var(--muted); margin-top: .5rem; }
.archived { margin-top: .5rem; }
.archived summary { cursor: pointer; color: var(--muted); padding: .4rem 0; }
.deleted-list { display: flex; flex-direction: column; gap: .35rem; }
.deleted-row { display: flex; justify-content: space-between; align-items: center; gap: .5rem; padding: .45rem .6rem; background: var(--surface-2); border-radius: 8px; }

/* Strain / pheno tree --------------------------------------------------- */
.strain-list { display: flex; flex-direction: column; gap: .7rem; }
.strain-card-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; }
.strain-name { font-weight: 700; font-size: 1.05rem; color: var(--text); }
.strain-card-actions { display: flex; gap: .3rem; }
.pheno-rows { display: flex; flex-direction: column; gap: .3rem; margin-top: .5rem; }
.pheno-row {
  display: flex; align-items: center; gap: .5rem; padding: .45rem .6rem;
  background: var(--surface-2); border-radius: 8px; color: var(--text);
}
.pheno-row-link {
  display: flex; align-items: center; gap: .5rem; flex: 1 1 auto;
  color: var(--text); min-width: 0;
}
.pheno-row-link:hover { text-decoration: none; }
.pheno-row:hover { outline: 1px solid var(--brand); }
.pheno-edit-btn { flex: 0 0 auto; }
.pheno-label { font-weight: 600; }
.pheno-qty { color: var(--muted); font-size: .85rem; }
.pheno-qty-lg { color: var(--muted); font-weight: 400; font-size: 1rem; margin-right: .4rem; }

/* Badges ------------------------------------------------------------------- */
.badge { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: .18rem .4rem; border-radius: 5px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge-muted { background: var(--surface-2); color: var(--muted); }
.badge-kill { background: var(--danger); color: #fff; }
.badge-herm { background: var(--danger-soft); color: var(--danger); border: 1px solid var(--danger); }
.badge-herm-confirmed { background: var(--danger); color: #fff; }
.stage-seedling { background: #e7f5d8; color: #4a7a1e; }
.stage-veg { background: #d8f0e6; color: #1f7a5c; }
.stage-flower { background: #f3e0f0; color: #8a3d7f; }
.stage-mother { background: #e0e8f5; color: #3d5a8a; }
.stage-clone { background: #fdeecf; color: #9a6a1a; }
.room-badge { letter-spacing: .04em; }
.room-veg { background: #d8f0e6; color: #1f7a5c; }
.room-flower { background: #f3e0f0; color: #8a3d7f; }
.room-other { background: var(--surface-2); color: var(--muted); }

/* By-room overview ----------------------------------------------------- */
.rooms-grid { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.room-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .9rem; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: .55rem;
}
.room-card-head { display: flex; align-items: center; gap: .5rem; }
.room-card-head h3 { margin: 0; font-size: 1rem; }
.room-batches { display: flex; flex-direction: column; gap: .4rem; }
.room-batch {
  display: block; padding: .5rem .6rem; background: var(--surface-2);
  border-radius: 8px; color: var(--text);
}
.room-batch:hover { text-decoration: none; outline: 1px solid var(--brand); }
.room-batch-top { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.room-batch-meta { display: flex; flex-wrap: wrap; gap: .3rem .7rem; font-size: .78rem; color: var(--muted); margin-top: .25rem; }
.room-stage-mix { display: flex; flex-wrap: wrap; gap: .25rem; margin-top: .35rem; }
.stage-count { display: inline-flex; align-items: center; gap: .25rem; font-size: .7rem; }

/* Export page -------------------------------------------------------------- */
.export-dates { display: flex; gap: .8rem; flex-wrap: wrap; }
.export-dates > * { flex: 1 1 140px; }

/* Stage tabs ------------------------------------------------------------- */
.stage-tabs { display: flex; flex-wrap: wrap; gap: .3rem; }
.stage-tab {
  font: inherit; cursor: pointer; text-transform: capitalize;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  padding: .45rem .8rem; border-radius: 999px;
}
.stage-tab[aria-current="true"] { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.stage-panels { display: flex; flex-direction: column; gap: 1rem; }

/* Ratings -------------------------------------------------------------------- */
.rating-rows { display: flex; flex-direction: column; gap: .6rem; }
.rating-row { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.rating-cat { min-width: 8rem; font-weight: 600; flex: 1 1 8rem; }
.rating-btns { display: inline-flex; gap: .2rem; }
.rating-btn {
  font: inherit; cursor: pointer; width: 2.2rem; height: 2.2rem; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.rating-btn[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 700; }
.rating-note { flex: 1 1 10rem; min-width: 8rem; }

/* Consensus --------------------------------------------------------------- */
.consensus-rows { display: flex; flex-direction: column; gap: .8rem; }
.consensus-row { display: grid; grid-template-columns: minmax(9rem, 1fr) minmax(8rem, 1.4fr) auto; gap: .6rem; align-items: center; }
.consensus-cat { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.consensus-stats { font-size: .8rem; color: var(--muted); display: flex; flex-direction: column; gap: .2rem; align-items: flex-end; }
.composite { font-size: .9rem; color: var(--muted); display: inline-flex; align-items: center; gap: .3rem; }
.mean-pill { font-weight: 700; padding: .15rem .5rem; border-radius: 6px; color: #10240f; font-size: .85rem; }
.raters summary { cursor: pointer; }
.raters ul { margin: .3rem 0 0; padding-left: 1rem; }

.dist { display: flex; height: 1.5rem; border-radius: 6px; overflow: hidden; border: 1px solid var(--line); }
.dist-seg { display: flex; align-items: center; justify-content: center; font-size: .72rem; color: #10240f; min-width: 0; }
.dist-seg span { padding: 0 .1rem; }
.dist-1 { background: var(--score-1); } .dist-2 { background: var(--score-2); }
.dist-3 { background: var(--score-3); } .dist-4 { background: var(--score-4); }
.dist-5 { background: var(--score-5); }
.dist-empty { font-size: .8rem; color: var(--muted); }

.score-none { color: var(--muted); }
.score-1 { background: color-mix(in srgb, var(--score-1) 26%, var(--surface)); }
.score-2 { background: color-mix(in srgb, var(--score-2) 26%, var(--surface)); }
.score-3 { background: color-mix(in srgb, var(--score-3) 30%, var(--surface)); }
.score-4 { background: color-mix(in srgb, var(--score-4) 30%, var(--surface)); }
.score-5 { background: color-mix(in srgb, var(--score-5) 34%, var(--surface)); }
span.mean-pill.score-1, span.mean-pill.score-2, span.mean-pill.score-3, span.mean-pill.score-4, span.mean-pill.score-5 { color: #10240f; }

/* Tables ------------------------------------------------------------------- */
.table-scroll { overflow-x: auto; }
table.matrix, table.admin-table { border-collapse: collapse; width: 100%; font-size: .9rem; }
table.matrix th, table.matrix td, table.admin-table th, table.admin-table td {
  border: 1px solid var(--line); padding: .4rem .55rem; text-align: left; white-space: nowrap;
}
table.matrix thead th { background: var(--surface-2); position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
td.score-cell { text-align: center; font-variant-numeric: tabular-nums; }
td.ft-cell { text-align: right; font-variant-numeric: tabular-nums; }
.ft-input { width: 6rem; padding: .3rem .4rem; }
td.score-cell.is-best { outline: 2px solid var(--brand); font-weight: 700; }
tr.row-kill td { background: var(--danger-soft); }
.row-actions { display: flex; gap: .3rem; flex-wrap: wrap; white-space: normal; }

.admin-table code { background: var(--surface-2); padding: .1rem .35rem; border-radius: 4px; }
.role-select { width: auto; padding: .25rem .4rem; font-size: .85rem; }
.ua-cell { max-width: 22rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .78rem; color: var(--muted); }
.secret {
  position: relative; display: inline-flex; flex-wrap: wrap; align-items: center; gap: .2rem .3rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem;
  background: var(--surface-2); padding: .15rem .4rem; border-radius: 5px;
  cursor: pointer; user-select: none; -webkit-user-select: none; touch-action: none;
  overflow: hidden; min-width: 6.5rem;
}
.secret.revealed { background: var(--brand-soft); cursor: default; }
.secret-text { letter-spacing: .06em; }
.secret-history { flex-basis: 100%; display: flex; flex-direction: column; }
.secret-history:empty { display: none; }
.secret-old { font-size: .74rem; color: var(--muted); }
.secret-tooshort { color: var(--danger); }
.secret-bar {
  position: absolute; left: 0; bottom: 0; height: 3px; width: 0;
  background: var(--brand); transition: width .05s linear;
}
.secret.revealed .secret-bar { display: none; }

/* Kill --------------------------------------------------------------------- */
.kill-banner, .herm-banner { background: var(--danger-soft); border: 1px solid var(--danger); border-radius: 8px; padding: .5rem .7rem; display: flex; flex-wrap: wrap; gap: .4rem; align-items: baseline; }
.herm-banner strong { color: var(--danger); }
.kill-card { border-color: var(--danger); }
.kill-list { margin: 0; padding-left: 1.1rem; display: flex; flex-direction: column; gap: .3rem; }

/* Gallery --------------------------------------------------------------- */
.gallery { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); margin-top: .7rem; }
.tile { margin: 0; position: relative; background: var(--surface-2); border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.tile img { width: 100%; aspect-ratio: 1; object-fit: cover; cursor: zoom-in; display: block; }
.tile figcaption { padding: .3rem .4rem; font-size: .74rem; display: flex; flex-direction: column; gap: .1rem; }
.tile-x { position: absolute; top: .2rem; right: .2rem; border: 0; border-radius: 6px; background: rgba(0,0,0,.55); color: #fff; width: 1.5rem; height: 1.5rem; cursor: pointer; }
.tile-actions { display: flex; gap: .3rem; padding: .3rem .4rem .4rem; }
.tile-actions .btn { flex: 1; justify-content: center; }
.full-img { max-width: 100%; max-height: 70vh; display: block; margin: 0 auto; border-radius: 8px; }
.image-form { display: grid; gap: .5rem; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); align-items: end; }
.image-form .field-wide { grid-column: 1 / -1; }
.dropzone { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; border: 1px dashed var(--line); border-radius: 8px; padding: .55rem .7rem; }
.dropzone.drag { border-color: var(--brand); background: var(--brand-soft); }
.dropzone .field-hint { margin: 0; }
.image-queue { display: flex; flex-direction: column; gap: .25rem; margin: .5rem 0 0; }
.iq-row { display: flex; align-items: center; gap: .5rem; font-size: .82rem; padding: .3rem .5rem; background: var(--surface-2); border-radius: 6px; }
.iq-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iq-status { white-space: nowrap; color: var(--muted); }
.iq-done .iq-status { color: var(--score-5); }
.iq-failed .iq-status { color: var(--danger); }
.iq-x { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: 0 .2rem; }
.link-clear { align-self: flex-start; border: 0; background: none; color: var(--brand-dark); cursor: pointer; font-size: .8rem; padding: .2rem 0; }

/* Notes ------------------------------------------------------------------ */
.note-form { display: flex; gap: .4rem; align-items: flex-start; margin-bottom: .6rem; }
.note-form textarea { flex: 1; }
.notes-list { display: flex; flex-direction: column; gap: .5rem; }
.note-item { background: var(--surface-2); border-radius: 8px; padding: .5rem .7rem; }
.note-item p { margin: .2rem 0 0; white-space: pre-wrap; }
.note-meta { font-size: .78rem; color: var(--muted); display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

/* Stage log ------------------------------------------------------------- */
.stage-log { list-style: none; margin: 0 0 .6rem; padding: 0; display: flex; flex-direction: column; gap: .3rem; }
.stage-log li { display: flex; align-items: center; gap: .5rem; }
.stage-form { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; font-size: .85rem; }
.stage-form select, .stage-form input { width: auto; }

/* Charts --------------------------------------------------------------------- */
.chart-wrap { display: flex; flex-direction: column; gap: .4rem; }
.chart-scroll { overflow-x: auto; }
svg.chart { max-width: none; }
svg.chart .grid { stroke: var(--line); stroke-width: 1; }
svg.chart text.axis { fill: var(--muted); font-size: 10px; }
svg.chart .line-path { fill: none; stroke: var(--brand); stroke-width: 2; }
svg.chart .line-dot { fill: var(--brand); }
.legend { display: flex; flex-wrap: wrap; gap: .3rem .8rem; font-size: .8rem; }
.legend-item { display: inline-flex; align-items: center; gap: .3rem; }
.legend-dot { width: .7rem; height: .7rem; border-radius: 3px; display: inline-block; }
.trend { margin-top: 1rem; border-top: 1px solid var(--line); padding-top: .8rem; }

/* Pick list (compare) ------------------------------------------------------ */
.pick-list { display: flex; flex-direction: column; gap: .3rem; margin: .6rem 0; }
.pick-item { display: flex; gap: .5rem; align-items: center; padding: .4rem .5rem; background: var(--surface-2); border-radius: 8px; }
.pick-item input { width: auto; }
.compare-controls { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; }

/* Modal ------------------------------------------------------------------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(10, 20, 12, .45); display: grid; place-items: center; padding: 1rem; z-index: 50; }
.modal-card { background: var(--surface); border-radius: var(--radius); width: min(440px, 100%); max-height: 90vh; overflow: auto; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: .8rem 1rem; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; }
.modal-x { border: 0; background: none; font-size: 1.4rem; line-height: 1; cursor: pointer; color: var(--muted); }
.modal-body { padding: 1rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; padding: .8rem 1rem; border-top: 1px solid var(--line); flex-wrap: wrap; }

/* Toast ----------------------------------------------------------------- */
#toast-host { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: .4rem; z-index: 60; width: min(400px, 92vw); }
.toast { background: var(--text); color: var(--bg); padding: .6rem .9rem; border-radius: 9px; opacity: 0; transform: translateY(8px); transition: .25s; font-size: .9rem; }
.toast.show { opacity: 1; transform: none; }
.toast-error { background: var(--danger); color: #fff; }
.toast-success { background: var(--brand); color: #fff; }

@media (max-width: 620px) {
  .consensus-row { grid-template-columns: 1fr; }
  .consensus-stats { align-items: flex-start; }
  .nav-user { width: 100%; margin-left: 0; }
  .rating-cat { flex-basis: 100%; }
}
