:root {
  --paper: #f4f0e6;
  --paper-dark: #e8e0cc;
  --ink: #2e2a22;
  --soil: #6b4f2a;
  --soil-dark: #4a3519;
  --leaf: #4c7a3f;
  --leaf-dark: #355c2b;
  --terracotta: #b5562f;
  --amber: #c98a2c;
  --line: #d8cfb8;
  --muted: #8a7f68;
  --white: #fffdf8;
  --radius: 6px;
  --mono: 'JetBrains Mono', 'Courier New', monospace;
  --sans: 'Inter', -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--paper);
  color: var(--ink);
  background-image:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 28px 28px;
  background-attachment: fixed;
}

.view { min-height: 100vh; }
.view[hidden] { display: none; }

/* ---------- LOGIN ---------- */
#view-login {
  display: flex;
  align-items: center;
  justify-content: center;
}
#view-login[hidden] { display: none; }
.login-card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 36px 32px;
  width: 320px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(74, 53, 25, 0.12);
}
.login-mark { font-size: 40px; }
.login-card h1 { font-size: 19px; margin: 8px 0 2px; color: var(--soil-dark); }
.muted { color: var(--muted); font-size: 13px; }
#login-form { margin-top: 22px; display: flex; flex-direction: column; gap: 12px; text-align: left; }
#login-form label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
input, select, button {
  font-family: var(--sans);
  font-size: 14px;
}
input, select {
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--white);
  color: var(--ink);
}
button {
  cursor: pointer;
  border: none;
  border-radius: 4px;
  padding: 10px 14px;
  background: var(--leaf);
  color: var(--white);
  font-weight: 600;
  transition: background .15s;
}
button:hover { background: var(--leaf-dark); }
button.secondary { background: var(--soil); }
button.secondary:hover { background: var(--soil-dark); }
button.ghost { background: transparent; color: var(--soil); border: 1px solid var(--line); }
button.ghost:hover { background: var(--paper-dark); }
button.primary { background: var(--terracotta); }
button.primary:hover { background: #8f421f; }
.error { color: var(--terracotta); font-size: 13px; min-height: 16px; }

/* ---------- TOPBAR ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 20px;
  background: var(--white);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
  flex-wrap: wrap;
}
.brand { font-weight: 700; color: var(--soil-dark); white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab-btn {
  background: transparent; color: var(--muted); font-weight: 600;
  border-radius: 4px; padding: 8px 14px;
}
.tab-btn.active { background: var(--paper-dark); color: var(--soil-dark); }
.who { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.role-badge {
  background: var(--leaf); color: white; font-size: 11px; padding: 2px 8px;
  border-radius: 10px; text-transform: uppercase; letter-spacing: .03em;
}

/* ---------- GRID ---------- */
.tab-panel { padding: 18px 20px 60px; max-width: 1400px; margin: 0 auto; }
.grid-toolbar {
  display: flex; align-items: center; gap: 18px; margin-bottom: 14px; flex-wrap: wrap;
}
.legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; }
.legend-item { display: flex; align-items: center; gap: 5px; }
.legend-swatch { width: 12px; height: 12px; border-radius: 2px; display: inline-block; }
.hint { color: var(--muted); font-size: 12px; margin-top: 10px; }

.field-grid {
  display: grid;
  gap: 3px;
  background: var(--white);
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow-x: auto;
}
.field-row { display: flex; gap: 3px; align-items: center; }
.row-label { width: 38px; font-family: var(--mono); font-size: 11px; color: var(--muted); flex-shrink: 0; text-align: right; padding-right: 4px;}
.bush-cell {
  width: 38px; height: 34px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--mono); font-size: 11px; font-weight: 600;
  border-radius: 3px; cursor: pointer; color: white;
  border: 1px solid rgba(0,0,0,.15);
  position: relative;
  transition: transform .1s;
}
.bush-cell:hover { transform: scale(1.12); z-index: 2; }
.bush-cell.empty { background: var(--paper-dark); color: var(--muted); cursor: default; }
.bush-cell.empty:hover { transform: none; }
.bush-cell.draft { outline: 2px dashed var(--amber); outline-offset: -2px; }
.bush-cell .dot { position: absolute; bottom: 2px; right: 2px; width: 5px; height: 5px; border-radius: 50%; background: var(--amber); }

/* ---------- ANALYTICS ---------- */
.cards { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 24px; }
.card {
  background: var(--white); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 18px; min-width: 150px;
}
.card .num { font-size: 26px; font-weight: 700; color: var(--soil-dark); font-family: var(--mono); }
.card .lbl { font-size: 12px; color: var(--muted); }

.table-wrap { overflow-x: auto; background: var(--white); border: 1px solid var(--line); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { background: var(--paper-dark); color: var(--soil-dark); position: sticky; top: 0; }
tr:hover td { background: #faf6ea; }
table.compact th, table.compact td { padding: 6px 8px; font-size: 12px; }

/* ---------- ADMIN ---------- */
.admin-grid { display: flex; flex-direction: column; gap: 30px; }
.admin-block { background: var(--white); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.admin-block h2 { margin-top: 0; font-size: 16px; color: var(--soil-dark); }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.inline-form input, .inline-form select { flex: 1; min-width: 90px; }
.row-editor-controls { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }

#full-grid-wrap { max-height: 70vh; }
.fg-table { border-collapse: collapse; }
.fg-table th, .fg-table td { border: 1px solid var(--line); padding: 0; }
.fg-table thead th { position: sticky; top: 0; background: var(--paper-dark); font-size: 10px; color: var(--muted); font-family: var(--mono); z-index: 1; padding: 2px; }
.fg-rowhead { position: sticky; left: 0; font-size: 10px; font-family: var(--mono); color: white; padding: 2px 6px; white-space: nowrap; z-index: 1; }
.fg-table input {
  width: 44px; height: 26px; text-align: center; font-family: var(--mono); font-size: 11px;
  border: none; padding: 0; background: transparent;
}
.fg-table input:focus { background: var(--white); outline: 2px solid var(--leaf); z-index: 2; position: relative; }

/* ---------- MODAL ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(46,42,34,.55);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--paper); border-radius: var(--radius); padding: 24px;
  width: 560px; max-width: 100%; max-height: 90vh; overflow-y: auto;
  position: relative; border: 1px solid var(--line);
}
.modal-close {
  position: absolute; top: 12px; right: 12px; background: transparent; color: var(--muted);
  font-size: 22px; padding: 2px 8px; line-height: 1;
}
.modal-close:hover { background: var(--paper-dark); }
.modal h2 { margin: 0 0 2px; color: var(--soil-dark); font-family: var(--mono); }
.modal-tabs { display: flex; gap: 4px; margin: 16px 0; border-bottom: 1px solid var(--line); }
.mtab-btn { background: transparent; color: var(--muted); border-radius: 4px 4px 0 0; padding: 8px 12px; font-size: 13px; }
.mtab-btn.active { background: var(--white); color: var(--soil-dark); border: 1px solid var(--line); border-bottom: none; }
.events-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; max-height: 220px; overflow-y: auto; }
.event-item {
  background: var(--white); border: 1px solid var(--line); border-radius: 4px; padding: 8px 10px;
  font-size: 12.5px; display: flex; justify-content: space-between; gap: 8px;
}
.event-item .et { font-weight: 600; color: var(--soil-dark); }
.event-item .del { color: var(--terracotta); cursor: pointer; font-size: 12px; }
.stacked-form { display: flex; flex-direction: column; gap: 8px; }
.form-row { display: flex; gap: 8px; }
.form-row select { flex: 2; } .form-row input { flex: 1; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form-grid label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }

.admin-only, .editor-only { }
body.role-viewer .admin-only,
body.role-viewer .editor-only,
body.role-editor .admin-only { display: none !important; }

@media (max-width: 640px) {
  .modal { width: 100%; padding: 18px; }
  .form-grid { grid-template-columns: 1fr; }
  .topbar { gap: 10px; }
}
