/* Табель: узкая «линованная» сетка как в бумажной форме Т-13, всё управление — внизу под пальцем. */
:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --fg: #17201e;
  --muted: #5d6a67;
  --line: #d5dcda;
  --line-strong: #a9b5b2;
  --accent: #0f6b62;
  --accent-fg: #ffffff;
  --accent-soft: #dcefeb;
  --off: #e8ecef;
  --off-fg: #6a7680;
  --sel: #ffe9a8;
  --sel-line: #c58a00;
  --danger: #b3261e;

  --c-b: #c0362c;   --c-b-bg: #fbe4e1;
  --c-o: #2f7d32;   --c-o-bg: #e1f1e1;
  --c-k: #2456a6;   --c-k-bg: #e1eaf8;
  --c-ur: #9a6300;  --c-ur-bg: #fbefd6;
  --c-x: #6b3fa0;   --c-x-bg: #eee5f7;

  --font: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --radius: 10px;
  --name-w: 148px;
  --day-w: 34px;
  --row-h: 44px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111716;
    --surface: #182120;
    --fg: #e4ebe9;
    --muted: #93a19e;
    --line: #2b3735;
    --line-strong: #46534f;
    --accent: #4fc2b3;
    --accent-fg: #06201d;
    --accent-soft: #173a36;
    --off: #222b2e;
    --off-fg: #8a979e;
    --sel: #4a3d12;
    --sel-line: #e2b23a;
    --danger: #f2867d;
    --c-b: #f28b82;   --c-b-bg: #3d2120;
    --c-o: #81c784;   --c-o-bg: #1e3320;
    --c-k: #8ab4f8;   --c-k-bg: #1d2b44;
    --c-ur: #f0c060;  --c-ur-bg: #3a2f14;
    --c-x: #c5a3ef;   --c-x-bg: #2f2340;
    color-scheme: dark;
  }
}
@media (min-width: 900px) {
  :root { --name-w: 260px; --day-w: 36px; --row-h: 40px; }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.4 var(--font);
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface);
  font-weight: 500; white-space: nowrap; text-decoration: none; color: var(--fg);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.icon { padding: 0; width: 40px; }
.btn svg { width: 18px; height: 18px; flex: none; }

/* ---------- каркас ---------- */
.app { display: flex; flex-direction: column; height: 100%; }

.appbar {
  flex: none;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1 1 0;
  min-height: 40px; padding: 0 8px 0 0; border: 0; background: none; text-align: left;
}
.brand .lbl { font-weight: 700; letter-spacing: .02em; flex: none; }
.brand .nm {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-weight: 500; color: var(--accent);
}
.brand svg { width: 16px; height: 16px; flex: none; color: var(--accent); }
.brand:hover .nm { text-decoration: underline; text-underline-offset: 3px; }
.month-nav { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.month-nav .title { min-width: 120px; text-align: center; font-weight: 600; font-size: 16px; }
.actions { display: flex; gap: 8px; width: 100%; overflow-x: auto; scrollbar-width: none; }
.actions::-webkit-scrollbar { display: none; }
.actions .spacer { flex: 1; }
.save-state { font-size: 12px; color: var(--muted); align-self: center; white-space: nowrap; }
.save-state.err { color: var(--danger); }
@media (max-width: 480px) {
  .actions .btn { padding: 0 10px; font-size: 14px; }
  .actions .long { display: none; }
  .brand .lbl { display: none; }
  .month-nav .title { min-width: 0; font-size: 15px; }
  .month-nav .btn.icon { width: 32px; }
}
@media (min-width: 900px) {
  .actions { width: auto; order: 3; }
  .month-nav { margin-left: 0; order: 2; }
  .brand { order: 1; margin-right: auto; }
}

.sheet {
  flex: 1; min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  background: var(--surface);
}

/* ---------- таблица ---------- */
table.grid {
  border-collapse: separate; border-spacing: 0;
  font-variant-numeric: tabular-nums;
  user-select: none; -webkit-user-select: none;
}
.grid th, .grid td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0; text-align: center;
  background: var(--surface);
}
.grid thead th {
  position: sticky; top: 0; z-index: 2;
  height: 52px; font-weight: 500; font-size: 13px;
  border-bottom-color: var(--line-strong);
}
.grid .name-col {
  position: sticky; left: 0; z-index: 1;
  width: var(--name-w); min-width: var(--name-w); max-width: var(--name-w);
  text-align: left; border-right: 1px solid var(--line-strong);
}
.grid thead .name-col { z-index: 3; padding: 0 10px; color: var(--muted); font-size: 12px; }
.grid td.name-col { padding: 4px 10px; cursor: pointer; }
.grid td.name-col .n { font-weight: 500; font-size: 14px; line-height: 1.2; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.grid td.name-col .p { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.grid td.name-col:hover .n { color: var(--accent); }

.day-h { width: var(--day-w); min-width: var(--day-w); }
.day-h button {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 100%; height: 100%; border: 0; background: none; padding: 0; line-height: 1.1;
}
.day-h b { font-size: 14px; font-weight: 600; }
.day-h span { font-size: 10.5px; color: var(--muted); }
.day-h.off, .grid td.off { background: var(--off); }
.day-h.off b { color: var(--off-fg); }
.day-h.override button { box-shadow: inset 0 -3px 0 var(--accent); }
.day-h.today b { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }

.grid td.c {
  height: var(--row-h); width: var(--day-w); min-width: var(--day-w);
  font: 600 13px var(--mono); letter-spacing: -.02em;
  cursor: pointer;
}
.grid td.off { color: var(--off-fg); font-weight: 500; }
.grid td.k-Б { color: var(--c-b); background: var(--c-b-bg); }
.grid td.k-О { color: var(--c-o); background: var(--c-o-bg); }
.grid td.k-К { color: var(--c-k); background: var(--c-k-bg); }
.grid td.k-УР { color: var(--c-ur); background: var(--c-ur-bg); }
.grid td.k-ДО, .grid td.k-ПР { color: var(--c-x); background: var(--c-x-bg); }
.grid td.c.sel { background: var(--sel); box-shadow: inset 0 0 0 1px var(--sel-line); }
.grid td.c.anchor { box-shadow: inset 0 0 0 2px var(--sel-line); }

.tot { width: 56px; min-width: 56px; font-weight: 700; font-size: 14px; }
.grid thead .tot { font-size: 10.5px; font-weight: 500; line-height: 1.15; padding: 0 3px; color: var(--muted); }
.grid td.tot { background: var(--accent-soft); color: var(--accent); }
.grid td.tot.sub { background: transparent; color: var(--fg); }
.grid .tot.sub { border-left: 1px solid var(--line-strong); border-right-color: var(--line-strong); }
.load { width: 64px; min-width: 64px; }
.grid thead .load { font-size: 10.5px; color: var(--muted); }
.load input {
  width: 100%; height: var(--row-h); border: 0; background: transparent; text-align: center;
  font-variant-numeric: tabular-nums; font-weight: 500;
}
.load input::placeholder { color: var(--line-strong); }

.empty {
  max-width: 420px; margin: 48px auto; padding: 0 16px; text-align: center;
  display: grid; gap: 12px; justify-items: center; color: var(--muted);
}
.empty h2 { margin: 0; color: var(--fg); font-size: 18px; text-wrap: balance; }

/* ---------- нижняя панель кодов ---------- */
.dock {
  flex: none;
  background: var(--surface);
  border-top: 1px solid var(--line-strong);
  padding-top: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
  padding-left: max(12px, env(safe-area-inset-left, 0px));
  padding-right: max(12px, env(safe-area-inset-right, 0px));
  display: grid; gap: 8px;
}
.dock-info { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); min-height: 32px; }
.dock-info .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toggle {
  display: inline-flex; align-items: center; gap: 6px; padding: 0 10px; min-height: 32px;
  border: 1px solid var(--line-strong); border-radius: 999px; background: var(--surface); font-size: 13px;
}
.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.codes { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.codes::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 56px; height: 48px; padding: 0 8px;
  border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--surface);
}
.chip b { font: 600 15px var(--mono); }
.chip span { font-size: 10px; color: var(--muted); white-space: nowrap; }
.chip:disabled { opacity: .45; cursor: default; }
.chip.k-Б b { color: var(--c-b); } .chip.k-О b { color: var(--c-o); } .chip.k-К b { color: var(--c-k); }
.chip.k-УР b { color: var(--c-ur); } .chip.k-ДО b, .chip.k-ПР b { color: var(--c-x); }
.chip.clear b { font-family: var(--font); }

/* ---------- диалоги: снизу на телефоне, по центру на десктопе ---------- */
dialog {
  border: 0; padding: 0; color: var(--fg); background: var(--surface);
  width: 100%; max-width: 100%; max-height: 92dvh;
  margin: auto auto 0; border-radius: 16px 16px 0 0;
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
@media (min-width: 640px) {
  dialog { width: 520px; margin: auto; border-radius: 16px; }
}
.dlg { display: flex; flex-direction: column; max-height: 92dvh; }
.dlg header { display: flex; align-items: center; gap: 8px; padding: 14px 16px 8px; }
.dlg header h2 { margin: 0; font-size: 18px; flex: 1; }
.dlg .body { padding: 4px 16px 16px; overflow: auto; display: grid; gap: 14px; }
.dlg footer {
  display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
}
.field { display: grid; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.field input, .field select {
  min-height: 44px; padding: 0 12px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--bg);
  width: 100%; min-width: 0;
}
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 36px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.hint { font-size: 13px; color: var(--muted); margin: 0; }
.error { color: var(--danger); font-size: 13px; margin: 0; }

.emp-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: var(--radius); }
.emp-list li { display: flex; align-items: center; gap: 4px; padding: 6px 6px 6px 12px; border-bottom: 1px solid var(--line); }
.emp-list li:last-child { border-bottom: 0; }
.emp-list .who { flex: 1; min-width: 0; }
.emp-list .who b { display: block; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.emp-list .who small { color: var(--muted); }
.emp-picks { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 12px; }
.code-pick { display: flex; flex-wrap: wrap; gap: 6px; }
.code-pick .chip[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--accent-soft); }

.toast {
  position: fixed; left: 50%; bottom: calc(150px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  max-width: calc(100% - 32px); z-index: 50; pointer-events: none;
  opacity: 0; transition: opacity .2s;
}
.toast.show { opacity: 1; }
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }

/* ---------- вход ---------- */
.login-wrap {
  min-height: 100%; display: grid; place-items: center;
  padding-block: 24px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.login {
  width: 100%; max-width: 360px; display: grid; gap: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 24px 20px;
}
.login h1 { margin: 0; font-size: 22px; }
.login .lead { margin: 0 0 4px; color: var(--muted); font-size: 14px; }
.ruled {
  height: 28px; border-radius: 6px;
  background:
    repeating-linear-gradient(90deg, var(--line) 0 1px, transparent 1px 14px),
    linear-gradient(90deg, transparent 0 70%, var(--off) 70% 84%, transparent 84%);
}

.sub-h { margin: 4px 0 0; font-size: 15px; }
.body-group { display: grid; gap: 14px; }
.sheet-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.sheet-list .pick {
  width: 100%; display: grid; gap: 2px; text-align: left; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface);
}
.sheet-list .pick:hover { border-color: var(--accent); }
.sheet-list .pick.cur { border-color: var(--accent); background: var(--accent-soft); }
.sheet-list .pick b { font-weight: 600; }
.sheet-list .pick small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
[hidden] { display: none !important; }
