:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1b1f24;
  --muted: #5d6673;
  --line: #e1e5eb;
  --line-strong: #cfd5dd;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --danger: #cf222e;
  --ok: #1a7f37;
  --warn: #b35900;
  --today: #fff7d6;
  --weekend: #fafbfc;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 14px; }
[hidden] { display: none !important; }
h1 { font-size: 20px; margin: 0 0 0 8px; font-weight: 650; white-space: nowrap; }
h2 { font-size: 16px; margin: 0 0 6px; }
h3 { font-size: 14px; margin: 18px 0 6px; }
a { color: var(--accent); }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.logo { width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  background-image: linear-gradient(white, white), linear-gradient(white, white), linear-gradient(white, white);
  background-size: 12px 2px; background-repeat: no-repeat; background-position: 5px 6px, 5px 10px, 5px 14px; }
.tabs { display: flex; gap: 2px; }
.tabs button { border: 0; background: none; padding: 8px 12px; border-radius: 8px; font: inherit; color: var(--muted); cursor: pointer; font-weight: 550; }
.tabs button:hover { background: var(--bg); color: var(--ink); }
.tabs button.active { background: #e8f0fe; color: var(--accent); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }
.search { position: relative; flex: 1; min-width: 180px; max-width: 360px; margin-left: auto; }
.search input { width: 100%; }
.search-results { position: absolute; top: 38px; left: 0; right: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); max-height: 60vh; overflow: auto; z-index: 30; }
.search-results .item { padding: 8px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: flex; gap: 8px; align-items: center; }
.search-results .item:hover { background: var(--bg); }
.search-results .empty { padding: 12px; color: var(--muted); }
.who { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.who .name { font-weight: 600; }
.who .role { color: var(--muted); font-size: 12px; }

main { padding: 16px; max-width: 1500px; margin: 0 auto; }

/* Controls */
input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: 7px; background: #fff; color: var(--ink); }
input:focus, select:focus, textarea:focus { outline: 2px solid #b6d0fb; border-color: var(--accent); }
textarea { resize: vertical; width: 100%; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-strong); background: #fff; color: var(--ink); padding: 7px 12px;
  border-radius: 7px; font: inherit; font-weight: 550; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(.95); }
.btn.danger { color: var(--danger); border-color: #f0b9bd; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.icon { width: 34px; justify-content: center; font-size: 18px; padding: 4px 0; }
.btn:disabled { opacity: .5; cursor: default; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 7px; overflow: hidden; }
.seg button { border: 0; background: #fff; padding: 7px 12px; font: inherit; cursor: pointer; color: var(--muted); font-weight: 550; }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button.active { background: #e8f0fe; color: var(--accent); }
.hint { color: var(--muted); margin: 0 0 12px; line-height: 1.45; }
.sub { color: var(--muted); font-weight: 400; font-size: 12px; }
body:not(.can-edit) .editor-only { display: none !important; }

/* Calendar */
.cal-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.cal-nav, .cal-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.legend button { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); border-radius: 20px;
  padding: 3px 10px 3px 4px; font: inherit; font-size: 12px; cursor: pointer; color: var(--ink); }
.legend button .sw { width: 16px; height: 16px; border-radius: 50%; border: 1px solid rgba(0,0,0,.15); }
.legend button.off { opacity: .4; text-decoration: line-through; }
.month { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); }
.month .dow { padding: 8px; font-weight: 650; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); border-bottom: 1px solid var(--line); background: #fbfcfd; }
.day { min-height: 128px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 4px; display: flex; flex-direction: column; gap: 3px; position: relative; }
.day:nth-child(7n) { border-right: 0; }
.day.weekend { background: var(--weekend); }
.day.other { background: #f6f7f9; }
.day.other .num { color: #a6adb7; }
.day.today { background: var(--today); }
.day.today .num { background: var(--accent); color: #fff; }
.day.drop { outline: 2px dashed var(--accent); outline-offset: -3px; }
.day .head { display: flex; justify-content: space-between; align-items: center; }
.day .num { font-weight: 650; font-size: 12px; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; }
.day .add { opacity: 0; border: 0; background: none; color: var(--accent); font-size: 18px; line-height: 1; cursor: pointer; padding: 0 4px; }
body.can-edit .day:hover .add { opacity: 1; }
.chip { display: block; width: 100%; text-align: left; border: 0; border-radius: 5px; padding: 3px 6px; font: inherit; font-size: 12px; line-height: 1.3; cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.08); overflow: hidden; }
.chip:hover { filter: brightness(.96); box-shadow: inset 0 0 0 1px rgba(0,0,0,.25); }
.chip .so { opacity: .75; }
.chip .t { font-weight: 650; }
.chip .flags { float: right; margin-left: 4px; }
.chip.unconfirmed { box-shadow: inset 3px 0 0 #e8590c, inset 0 0 0 1px rgba(0,0,0,.08); }
.chip.loan { background: #fff; border: 1px dashed #00a3ad; color: #00727a; box-shadow: none; }
.chip.loan.overdue { border-color: var(--danger); color: var(--danger); background: #fff5f5; }
.chip[draggable=true] { cursor: grab; }

/* List view */
.list { display: flex; flex-direction: column; gap: 12px; }
.list .lday { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list .lday.today { border-color: var(--accent); }
.list .lday h3 { margin: 0; padding: 8px 12px; background: #fbfcfd; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; }
.list .row-job { display: grid; grid-template-columns: 6px 1fr auto; gap: 10px; padding: 8px 12px 8px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.list .row-job:last-child { border-bottom: 0; }
.list .row-job:hover { background: var(--bg); }
.list .bar { border-radius: 0 3px 3px 0; }
.list .detail { color: var(--muted); font-size: 13px; }
.list .status { font-size: 12px; font-weight: 650; align-self: center; }
.ok { color: var(--ok); } .warn { color: var(--warn); } .bad { color: var(--danger); }

/* Tables */
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
table.grid { width: 100%; border-collapse: collapse; }
table.grid th, table.grid td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.grid th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #fbfcfd; font-weight: 650; }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: var(--bg); }
table.grid.compact td, table.grid.compact th { padding: 6px 8px; }
table.grid.compact tbody tr { cursor: default; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 650; }
.pill.out { background: #e6f7f8; color: #00727a; }
.pill.soon { background: #fff4e5; color: var(--warn); }
.pill.overdue { background: #ffebe9; color: var(--danger); }
.pill.back { background: #eaf6ec; color: var(--ok); }
.empty-state { padding: 32px; text-align: center; color: var(--muted); }

/* History feed */
.feed { list-style: none; margin: 0; padding: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.feed li { padding: 10px 14px; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: start; }
.feed li:last-child { border-bottom: 0; }
.feed .av { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 12px; color: #fff; }
.feed .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.feed .changes { margin: 4px 0 0; font-size: 12px; color: var(--muted); }
.feed .changes del { color: #a4282f; background: #ffebe9; text-decoration: line-through; padding: 0 3px; border-radius: 3px; }
.feed .changes ins { color: #116329; background: #dafbe1; text-decoration: none; padding: 0 3px; border-radius: 3px; }
.feed.small { box-shadow: none; max-height: 260px; overflow: auto; }
.feed.small li { grid-template-columns: 1fr auto; padding: 8px 10px; }
#historyMore { margin-top: 12px; }

/* Settings */
.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.card label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-weight: 550; }
.card label.check, .dialog .body label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 450; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > label { flex: 0 1 auto; }
.row > .grow { flex: 1 1 180px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.notice { background: #f0f6ff; border: 1px solid #c8dcfb; padding: 8px 12px; border-radius: 8px; margin: 0 0 12px; line-height: 1.4; }
.notice.warn { background: #fff8eb; border-color: #f6d79a; }
.steps { margin: 0 0 12px; padding-left: 20px; line-height: 1.6; color: var(--muted); }
.result { background: #f6f8fa; border-radius: 8px; padding: 10px; max-height: 240px; overflow: auto; font-size: 12px; }
.swatch-input { width: 42px; height: 30px; padding: 2px; }

/* Dialogs */
.dialog { border: 0; border-radius: 14px; padding: 0; width: min(640px, calc(100vw - 24px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); color: var(--ink); }
.dialog.narrow { width: min(420px, calc(100vw - 24px)); }
.dialog.wide { width: min(820px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(15, 23, 42, .45); }
.dialog header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dialog header h2 { margin: 0; display: flex; align-items: center; gap: 8px; }
.dialog .x { border: 0; background: none; font-size: 24px; cursor: pointer; color: var(--muted); line-height: 1; }
.dialog .body { padding: 14px 18px; max-height: calc(100vh - 190px); overflow: auto; }
.dialog .body label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-weight: 550; }
.dialog .body input:not([type=checkbox]), .dialog .body select { width: 100%; }
.dialog footer { display: flex; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); background: #fbfcfd; }
.dialog .spacer { flex: 1; }
.dialog .meta { color: var(--muted); font-size: 12px; margin: 8px 0; }
.dialog .confirm { background: #f6f8fa; padding: 8px 10px; border-radius: 8px; }
.dialog .quick { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.dialog .hist summary { cursor: pointer; color: var(--accent); font-weight: 550; margin: 6px 0; }
.dialog fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.dialog fieldset:disabled input, .dialog fieldset:disabled select, .dialog fieldset:disabled textarea { background: #f6f8fa; color: var(--ink); border-color: var(--line); }
#previewFrame { width: 100%; height: 70vh; border: 0; display: block; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #1b1f24; color: #fff; padding: 10px 16px; border-radius: 8px;
  box-shadow: var(--shadow); z-index: 100; max-width: calc(100vw - 32px); }
.toast.error { background: var(--danger); }

@media (max-width: 760px) {
  main { padding: 12px; }
  .topbar { gap: 8px; padding: 8px 12px; }
  .tabs { order: 3; width: 100%; overflow-x: auto; }
  .search { order: 4; max-width: none; width: 100%; flex-basis: 100%; }
  .settings { grid-template-columns: 1fr; }
  h1 { font-size: 17px; }
  .day { min-height: 70px; }
  .legend { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .legend button { flex: none; }
  .tabs button { white-space: nowrap; }
}
