:root {
  --bg: #f4f6f4;
  --panel: #ffffff;
  --ink: #18221c;
  --muted: #5d6b62;
  --line: #dfe5e0;
  --line-strong: #c9d2cb;
  --accent: #1f6f43;
  --accent-ink: #ffffff;
  --accent-soft: #e5f2ea;
  --danger: #b3261e;
  --amber: #fff3d6;   --amber-ink: #7a5300;
  --blue: #e3eefc;    --blue-ink: #1d4f91;
  --violet: #efe7fb;  --violet-ink: #5a2f9a;
  --teal: #dff4f1;    --teal-ink: #11665b;
  --green: #e3f3e6;   --green-ink: #1d6b33;
  --grey: #eceeed;    --grey-ink: #5f6663;
  --radius: 8px;
  --shadow: 0 10px 30px rgba(20, 40, 28, .16);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); display: flex; flex-direction: column; min-height: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---- buttons & inputs ---- */
.btn {
  border: 1px solid var(--line-strong); background: var(--panel); border-radius: var(--radius);
  padding: 7px 12px; cursor: pointer; white-space: nowrap; line-height: 1.2;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #e4b8b4; }
.btn.wide { width: 100%; padding: 10px; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { border: 0; background: none; font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--bg); }
input, select, textarea {
  border: 1px solid var(--line-strong); border-radius: 6px; padding: 7px 9px; background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 1px; border-color: var(--accent);
}
textarea { resize: vertical; min-height: 60px; }
label { display: grid; gap: 4px; font-weight: 500; }
label.check, label.toggle { display: flex; align-items: center; gap: 6px; font-weight: 400; white-space: nowrap; }
label.check input, label.toggle input { width: auto; }
.form-error { color: var(--danger); margin: 0; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel);
  border-bottom: 1px solid var(--line); position: relative; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 15px; white-space: nowrap; }
.brand img { width: 26px; height: 26px; }
.brand b { color: var(--accent); }
.brand-lg { font-size: 19px; justify-content: center; margin-bottom: 18px; }
.brand-lg img { width: 36px; height: 36px; }
.boards { display: flex; background: var(--bg); border-radius: 9px; padding: 3px; gap: 2px; }
.boards button {
  border: 0; background: none; padding: 6px 14px; border-radius: 7px; cursor: pointer; color: var(--muted); font-weight: 600;
}
.boards button[aria-current="true"] { background: var(--panel); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.user-btn { display: flex; gap: 6px; align-items: center; }
.user-btn .role { color: var(--muted); font-size: 12px; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); min-width: 190px; padding: 6px; z-index: 30;
}
.menu > button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px;
  border-radius: 6px; cursor: pointer; }
.menu > button:hover { background: var(--bg); }
.columns-menu { max-height: 60vh; overflow: auto; min-width: 240px; }
.columns-menu label { padding: 5px 8px; font-weight: 400; }

/* ---- toolbar ---- */
.toolbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button {
  border: 1px solid transparent; background: none; padding: 6px 12px; border-radius: 999px; cursor: pointer; color: var(--muted);
}
.tabs button[aria-current="true"] { background: var(--accent-soft); color: var(--accent); border-color: #bfdccb; font-weight: 600; }
.tabs .count { font-size: 12px; margin-left: 4px; opacity: .8; }
.search { width: 300px; max-width: 100%; }

/* ---- grid ---- */
.workspace { flex: 1; min-height: 0; position: relative; padding: 10px 14px 14px; display: flex; flex-direction: column; }
.grid { flex: 1; min-height: 300px; }
.empty { text-align: center; color: var(--muted); margin: 40px 0; }
.tabulator { border: 1px solid var(--line); border-radius: var(--radius); font-size: 13px; background: var(--panel); }
.tabulator .tabulator-header { background: #f7f9f7; border-bottom: 1px solid var(--line-strong); }
.tabulator .tabulator-header .tabulator-col { background: #f7f9f7; }
.tabulator .tabulator-header .tabulator-col .tabulator-col-content { padding: 7px 8px; }
.tabulator .tabulator-col-title { white-space: normal !important; font-weight: 600; font-size: 12px; color: #39463e; line-height: 1.25; }
.tabulator-row .tabulator-cell { padding: 6px 8px; border-right: 1px solid #edf0ee; white-space: pre-wrap; overflow-wrap: anywhere; }
.tabulator-row { border-bottom: 1px solid #edf0ee; }
.tabulator-row.tabulator-row-even { background: #fbfcfb; }
.tabulator-row:hover { background: #f1f7f3 !important; }
.tabulator-row .tabulator-cell.tabulator-editing { border: 2px solid var(--accent); padding: 0; }
.tabulator-row .tabulator-cell.tabulator-editing textarea,
.tabulator-row .tabulator-cell.tabulator-editing input { border: 0; border-radius: 0; padding: 6px 8px; min-height: 100%; }
.tabulator-row .tabulator-cell.readonly-cell { color: #4a564f; }
.tabulator-row.row-urgent .tabulator-cell:first-child { box-shadow: inset 3px 0 0 #d9480f; }
.tabulator-row.row-flash { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: #fff1b8; } to { background: transparent; } }
.tabulator .tabulator-cell.tabulator-range-selected:not(.tabulator-range-only-cell-selected) { background: #dcefe3; }
.tabulator .tabulator-cell.tabulator-range-only-cell-selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.open-link { color: var(--accent); text-decoration: none; font-weight: 600; }
.open-link:hover { text-decoration: underline; }
a.maps { color: var(--blue-ink); }

.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.amber { background: var(--amber); color: var(--amber-ink); }
.pill.blue { background: var(--blue); color: var(--blue-ink); }
.pill.violet { background: var(--violet); color: var(--violet-ink); }
.pill.teal { background: var(--teal); color: var(--teal-ink); }
.pill.green { background: var(--green); color: var(--green-ink); }
.pill.grey, .pill.none { background: var(--grey); color: var(--grey-ink); }
.urgent { color: #c2410c; font-weight: 700; }

/* ---- cards (phones) ---- */
.cards { display: grid; gap: 8px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; text-align: left;
  display: grid; gap: 5px; cursor: pointer; width: 100%;
}
.card .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.card .title { font-weight: 600; }
.card .route { color: var(--muted); font-size: 13px; }
.card .meta { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }

/* ---- drawer ---- */
.scrim { position: fixed; inset: 0; background: rgba(15, 25, 20, .25); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 100%); background: var(--panel); z-index: 50;
  box-shadow: var(--shadow); display: flex; flex-direction: column; overflow-y: auto;
}
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; padding: 14px 16px 6px;
  position: sticky; top: 0; background: var(--panel); z-index: 1; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 2px 0 6px; font-size: 17px; line-height: 1.3; }
.drawer-code { color: var(--muted); font-size: 12px; display: flex; gap: 8px; align-items: center; }
.drawer-body { padding: 12px 16px; display: grid; gap: 10px; }
.drawer-body .ro { padding: 7px 0; color: var(--muted); }
.drawer-foot { display: flex; gap: 8px; padding: 6px 16px 12px; flex-wrap: wrap; }
.history { padding: 6px 16px 24px; border-top: 1px solid var(--line); }
.history h3 { font-size: 14px; margin: 10px 0; }
.history ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.history li { font-size: 13px; border-left: 3px solid var(--line-strong); padding-left: 9px; }
.history .when { color: var(--muted); font-size: 12px; }
.history s { color: #8b2f2a; }
.field-changed { border-color: #e0a800 !important; background: #fffbea; }

/* ---- dialog ---- */
dialog { border: 0; border-radius: 12px; padding: 0; box-shadow: var(--shadow); width: min(640px, calc(100% - 24px)); max-height: 90vh; }
dialog::backdrop { background: rgba(15, 25, 20, .35); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; background: var(--panel); z-index: 1; }
.dialog-head h2 { margin: 0; font-size: 17px; }
.dialog-body { padding: 14px 18px; display: grid; gap: 12px; }
.dialog-body .two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line);
  position: sticky; bottom: 0; background: var(--panel); }
.req::after { content: " *"; color: var(--danger); }
dialog.wide { width: min(1000px, calc(100% - 24px)); }
.users-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.users-table th, .users-table td { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.users-table tr.inactive td { color: var(--muted); }
.users-table .actions { white-space: nowrap; }

/* ---- toasts ---- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; }
.toast { background: #1d2a22; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 90vw; }
.toast.error { background: var(--danger); }

/* ---- login ---- */
.login-body { align-items: center; justify-content: center; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: min(380px, 100%);
  box-shadow: 0 6px 24px rgba(20, 40, 28, .08); }
.login-card form { display: grid; gap: 14px; }
.login-card p { text-align: center; margin: 14px 0 0; }

/* ---- phones ---- */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .brand span { display: none; }
  .boards { order: 3; width: 100%; }
  .boards button { flex: 1; }
  .user-btn .role { display: none; }
  .toolbar { padding: 8px 12px; }
  .tabs { width: 100%; flex-wrap: nowrap; overflow-x: auto; }
  .toolbar .spacer { display: none; }
  .search { width: auto; flex: 1; }
  #columns-btn, #export { display: none; }
  .workspace { padding: 8px 12px 80px; }
  #new-row { position: fixed; right: 16px; bottom: 16px; z-index: 20; padding: 12px 18px; border-radius: 999px; box-shadow: var(--shadow); }
  .dialog-body .two { grid-template-columns: 1fr; }
  .users-table thead { display: none; }
  .users-table tr { display: grid; padding: 6px 0; border-bottom: 1px solid var(--line); }
  .users-table td { border: 0; padding: 2px 6px; }
}

/* ---- admin ---- */
.admin-tabs { padding: 10px 18px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.admin-tabs [data-admin-users] { margin-left: auto; }
.admin-scroll { overflow-x: auto; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tile { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; background: #fbfcfb; }
.tile-v { font-size: 22px; font-weight: 700; }
.tile-k { color: var(--muted); font-size: 12px; }
.dialog-body h3 { margin: 6px 0 0; font-size: 15px; }
a.tel { color: var(--accent); }
