:root {
  --green: #1f7a4d;
  --green-dark: #155c39;
  --green-light: #e8f3ec;
  --bg: #f4f6f5;
  --card: #ffffff;
  --ink: #1b2420;
  --muted: #6b7a72;
  --line: #e2e8e4;
  --accent: #c9962b;
  --danger: #b3261e;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.45;
}

#app { display: flex; min-height: 100vh; }

/* Sidebar -------------------------------------------------------------- */
#sidebar {
  width: 230px;
  background: var(--green-dark);
  color: #dfeee6;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  flex-shrink: 0;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 18px 10px; }
.brand .leaf { font-size: 26px; }
.brand-name { font-weight: 700; font-size: 15px; color: #fff; }
.brand-sub { font-size: 11px; opacity: .7; letter-spacing: .08em; text-transform: uppercase; }
#nav { padding: 8px; flex: 1; overflow-y: auto; }
#nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; margin: 2px 0;
  border-radius: 8px; color: #cfe4d8; text-decoration: none;
  font-weight: 500; font-size: 13.5px;
}
#nav a .ico { width: 18px; text-align: center; }
#nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
#nav a.active { background: var(--green); color: #fff; }
#nav .nav-sep { font-size: 10px; text-transform: uppercase; letter-spacing: .1em;
  opacity: .5; padding: 14px 12px 4px; }
.sidebar-foot { padding: 10px; }
.link-btn { background: none; border: none; color: #9fc3b1; cursor: pointer; font-size: 12px; }
#sidebar-backdrop { display: none; position: fixed; inset: 0; z-index: 35; background: rgba(20,30,25,.42); }

/* Main ---------------------------------------------------------------- */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 22px; background: var(--card);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
#page-title { font-size: 18px; font-weight: 700; }
.topbar-actions { margin-left: auto; }
#global-search { width: 280px; max-width: 40vw; }
.icon-btn { display: none; background: none; border: none; font-size: 20px; cursor: pointer; }
#view { padding: 22px; max-width: 1200px; }

/* Generic ------------------------------------------------------------- */
h2 { margin: 0 0 4px; font-size: 17px; }
.section-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.section-head .sub { color: var(--muted); font-size: 13px; }
.section-head .spacer { flex: 1; }
.loading { color: var(--muted); padding: 40px; text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.card h3 { margin: 0 0 10px; font-size: 14px; }

.grid { display: grid; gap: 14px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px,1fr)); gap: 14px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; }
.stat .n { font-size: 26px; font-weight: 700; color: var(--green-dark); }
.stat .l { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* Buttons ------------------------------------------------------------- */
button, .btn {
  font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); padding: 8px 13px; font-weight: 600;
}
button:hover, .btn:hover { border-color: #c2cdc6; }
.btn-primary { background: var(--green); color: #fff; border-color: var(--green); }
.btn-primary:hover { background: var(--green-dark); border-color: var(--green-dark); }
.btn-sm { padding: 5px 9px; font-size: 12.5px; font-weight: 600; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--green-dark); }
.btn-danger { color: var(--danger); }

/* Forms --------------------------------------------------------------- */
input, select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; color: var(--ink); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green-light); border-color: var(--green); }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }

/* Tables -------------------------------------------------------------- */
table { width: 100%; border-collapse: collapse; background: var(--card); }
.table-wrap { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow-x: auto; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: middle; }
th { background: #fafbfa; color: var(--muted); font-weight: 600; font-size: 11.5px;
  text-transform: uppercase; letter-spacing: .03em; position: sticky; top: 0; }
tbody tr:hover { background: #fafcfb; }
td.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 600; }
a.lotlink { color: var(--green-dark); font-weight: 600; text-decoration: none; font-family: ui-monospace, Menlo, monospace; }
a.lotlink:hover { text-decoration: underline; }

/* Badges -------------------------------------------------------------- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.badge.stage-initiation { background: #eef2ff; color: #3a4ab0; }
.badge.stage-multiplication { background: var(--green-light); color: var(--green-dark); }
.badge.stage-rooting { background: #fff1e0; color: #9a6212; }
.badge.stage-primary_hardening { background: #e7f6f7; color: #1d6f78; }
.badge.stage-secondary_hardening { background: #f1ecf9; color: #6b4a9c; }
.badge.status-active { background: var(--green-light); color: var(--green-dark); }
.badge.status-subcultured { background: #eef1ef; color: var(--muted); }
.badge.status-shipped { background: #fdeef0; color: var(--danger); }
.badge.status-discarded { background: #f3f3f3; color: #999; }
.badge.s-ready { background: var(--green-light); color: var(--green-dark); }
.badge.s-curing { background: #fff1e0; color: #9a6212; }
.badge.s-depleted, .badge.s-discarded { background: #f1f1f1; color: #888; }

/* Modal --------------------------------------------------------------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(20,30,25,.45);
  display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; }
.modal { background: #fff; border-radius: 12px; width: 540px; max-width: 100%;
  box-shadow: 0 12px 40px rgba(0,0,0,.25); max-height: 88vh; display: flex; flex-direction: column; }
.modal header { padding: 16px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; }
.modal header h3 { margin: 0; font-size: 15px; }
.modal header .x { margin-left: auto; background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal .body { padding: 18px; overflow-y: auto; }
.modal .foot { padding: 14px 18px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; }

/* Toast --------------------------------------------------------------- */
#toast-root { position: fixed; bottom: 20px; right: 20px; z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #1b2420; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow);
  font-size: 13px; animation: slidein .2s ease; }
.toast.ok { background: var(--green-dark); }
.toast.err { background: var(--danger); }
@keyframes slidein { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Lineage ------------------------------------------------------------- */
.chain { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 8px 0 4px; }
.chain .node { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; box-shadow: var(--shadow); }
.chain .node.mother { background: var(--accent); color: #fff; border-color: var(--accent); }
.chain .node.current { border: 2px solid var(--green); }
.chain .arrow { color: var(--muted); font-size: 18px; }
.node .nlabel { font-size: 11px; opacity: .8; }
.node .ncode { font-family: ui-monospace, Menlo, monospace; font-weight: 700; }
.tree { list-style: none; padding-left: 18px; border-left: 2px solid var(--line); margin: 6px 0; }
.tree li { margin: 6px 0; }

/* Labels / print ------------------------------------------------------ */
.label-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px,1fr)); gap: 12px; }
.qr-label { border: 1px dashed #bbb; border-radius: 8px; padding: 10px; text-align: center; background: #fff; }
.qr-label svg { width: 120px; height: 120px; }
.qr-label .qcode { font-family: ui-monospace, Menlo, monospace; font-weight: 700; margin-top: 6px; font-size: 13px; }
.qr-label .qmeta { font-size: 11px; color: var(--muted); }

#reader { width: 320px; max-width: 100%; }

/* Reports / registers ------------------------------------------------- */
.report-filters { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.report-filters .field { margin: 0; min-width: 150px; }
.report-filters .spacer { flex: 1; }
.report { margin-bottom: 24px; }
.report-head { margin-bottom: 8px; }
.report-lab { font-size: 18px; font-weight: 700; color: var(--green-dark); }
.report-title { font-size: 15px; font-weight: 600; }
.report-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.report-toolbar { margin: 8px 0; }

/* Responsive ---------------------------------------------------------- */
@media (max-width: 820px) {
  #sidebar { position: fixed; top: 0; bottom: 0; left: -250px; width: 240px; z-index: 40;
    height: 100%; transition: left .2s; box-shadow: 2px 0 24px rgba(0,0,0,.25); }
  #sidebar.open { left: 0; }
  #sidebar.open ~ #sidebar-backdrop { display: block; }
  .icon-btn { display: block; }
  #nav a { padding: 11px 12px; }            /* roomier tap targets */

  #topbar { padding: 10px 12px; gap: 10px; }
  #page-title { font-size: 16px; min-width: 0; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap; }
  #global-search { width: 44vw; max-width: 240px; }
  #view { padding: 16px 14px; }

  /* Stack the two-column page layouts; the inline grid styles need the override */
  .grid { grid-template-columns: 1fr !important; }
  .form-row, .form-row-3 { grid-template-columns: 1fr; }

  /* 16px inputs stop iOS Safari from zooming the page in on focus */
  input, select, textarea { font-size: 16px; }
}

@media print {
  #sidebar, #topbar, .no-print { display: none !important; }
  #view { padding: 0; max-width: none; }
  .qr-label { border: 1px solid #999; }

  /* Registers: each on its own page, repeat column headers, avoid splitting rows */
  section.report + section.report { page-break-before: always; }
  .report .table-wrap { box-shadow: none; border: none; overflow: visible; }
  .report thead { display: table-header-group; }
  .report tr { page-break-inside: avoid; }
  .report th { position: static; }   /* sticky headers break print pagination */
  .report th, .report td { border-color: #999; }
  .report-lab { color: #000; }
}

/* Auth: login screen + user menu ------------------------------------- */
.login-overlay {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: var(--green-dark); padding: 20px;
}
.login-card {
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow);
  width: 360px; max-width: 100%; padding: 26px 24px;
}
.login-brand { display: flex; gap: 10px; align-items: center; margin-bottom: 4px; }
.login-brand .leaf { font-size: 28px; }
.login-brand .brand-name { font-weight: 700; font-size: 16px; color: var(--ink); }
.login-brand .brand-sub { font-size: 11px; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; }
.login-sub { color: var(--muted); margin: 6px 0 18px; font-size: 13px; }
.login-card .field { margin-bottom: 14px; }
.login-card input { width: 100%; }
.login-card .btn-primary { width: 100%; margin-top: 6px; padding: 10px; }
.login-err { color: var(--danger); font-size: 12.5px; min-height: 18px; margin-bottom: 4px; }

.user-menu { display: inline-flex; align-items: center; gap: 8px; margin-left: 12px; }
.user-menu .user-name { font-weight: 600; font-size: 13px; }
.user-menu .user-role {
  font-size: 10px; text-transform: uppercase; letter-spacing: .06em;
  background: var(--green-light); color: var(--green-dark);
  padding: 2px 7px; border-radius: 999px;
}
@media (max-width: 720px) {
  .user-menu .user-name, .user-menu .user-role { display: none; }
}

/* Sandbox indicator shown when the page is opened with ?mode=test */
.mode-banner {
  position: fixed; bottom: 12px; right: 12px; z-index: 9999;
  background: #b45309; color: #fff;
  font-weight: 700; font-size: 12px; letter-spacing: .03em;
  padding: 6px 12px; border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
  pointer-events: none;
}
