:root {
  color-scheme: light;
  --surface-1:      #fcfcfb;
  --surface-2:      #ffffff;
  --page-plane:     #f9f9f7;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --gridline:       #e1e0d9;
  --border:         rgba(11,11,11,0.10);
  --series-blue:    #2a78d6;
  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;
  --status-neutral:  #898781;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page-plane);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}
button, select, input, textarea { font-family: inherit; font-size: inherit; color: inherit; }
h1, h2 { margin: 0; }

/* ---------- login ---------- */
.login-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 14px;
  padding: 32px; width: 320px; display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.06);
}
.login-card h1 { font-size: 18px; }
.login-sub { margin: 0 0 8px; color: var(--text-secondary); font-size: 13px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-secondary); }
.login-card input {
  padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1);
}
.form-error { color: var(--status-critical); font-size: 13px; }

/* ---------- layout ---------- */
.app { max-width: 1440px; margin: 0 auto; padding: 20px 24px 64px; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 18px; }
.topbar-left { display: flex; align-items: baseline; gap: 12px; }
.topbar h1 { font-size: 18px; }
.user-badge { font-size: 13px; color: var(--text-secondary); }
.topbar-right { display: flex; gap: 8px; }

.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px;
  padding: 8px 14px; font-size: 13px; cursor: pointer; color: var(--text-primary);
}
.btn:hover { background: var(--surface-1); }
.btn-primary { background: var(--series-blue); color: #fff; border-color: var(--series-blue); }
.btn-primary:hover { filter: brightness(0.95); }
.btn-ghost { background: transparent; }
.btn-small { padding: 6px 10px; font-size: 12px; }
.btn-danger { color: var(--status-critical); border-color: var(--status-critical); background: transparent; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat-tile {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
}
.stat-tile .stat-value { font-size: 22px; font-weight: 600; }
.stat-tile .stat-label { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }

/* ---------- toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
.toolbar input[type="search"], .toolbar select {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.toolbar input[type="search"] { flex: 1 1 220px; min-width: 180px; }
.toolbar #createBtn { margin-left: auto; }

/* ---------- table ---------- */
.table-wrap { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; padding: 10px 12px; color: var(--text-secondary); font-weight: 500;
  border-bottom: 1px solid var(--gridline); background: var(--surface-1);
}
tbody td { padding: 10px 12px; border-bottom: 1px solid var(--gridline); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-1); cursor: pointer; }
.empty-state { padding: 40px; text-align: center; color: var(--text-muted); }

.badge {
  display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 500;
  color: #fff; white-space: nowrap;
}
.badge-active { background: var(--series-blue); }
.badge-pending_filial { background: var(--status-warning); color: #3a2c00; }
.badge-closed-good { background: var(--status-good); }
.badge-closed-bad { background: var(--status-critical); }
.badge-closed { background: var(--status-neutral); }

.owner-chip { font-size: 12px; color: var(--text-secondary); }
.stale-flag { color: var(--status-critical); font-weight: 600; margin-left: 6px; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(11,11,11,0.35); display: flex; align-items: center;
  justify-content: center; z-index: 50; padding: 20px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface-2); border-radius: 14px; width: 560px; max-width: 100%; max-height: 90vh;
  overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}
.modal-wide { width: 760px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between; padding: 16px 20px;
  border-bottom: 1px solid var(--gridline); position: sticky; top: 0; background: var(--surface-2);
}
.modal-close { border: none; background: none; font-size: 22px; cursor: pointer; color: var(--text-muted); line-height: 1; }
.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.field.full { grid-column: 1 / -1; }
.field label { color: var(--text-secondary); }
.field input, .field select, .field textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1);
}
.field textarea { resize: vertical; min-height: 60px; }
.field[data-readonly="1"] input, .field[data-readonly="1"] select, .field[data-readonly="1"] textarea {
  background: var(--page-plane); color: var(--text-muted);
}

.status-row { display: flex; flex-wrap: wrap; gap: 6px; }
.status-pill {
  border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px; font-size: 12px;
  background: var(--surface-1); cursor: pointer;
}
.status-pill.is-current { border-color: var(--series-blue); background: var(--series-blue); color: #fff; }
.status-pill:disabled { opacity: 0.4; cursor: not-allowed; }

.history-list { display: flex; flex-direction: column; gap: 8px; max-height: 220px; overflow-y: auto; }
.history-item { font-size: 12px; border-left: 2px solid var(--gridline); padding-left: 10px; }
.history-item .h-date { color: var(--text-muted); margin-right: 6px; }
.comment-row { display: flex; gap: 8px; }
.comment-row input { flex: 1; }

.admin-tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.admin-tab {
  border: 1px solid var(--border); background: var(--surface-1); border-radius: 8px; padding: 6px 12px;
  font-size: 13px; cursor: pointer;
}
.admin-tab.is-active { background: var(--series-blue); color: #fff; border-color: var(--series-blue); }
.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-bottom: 12px; }
.admin-table th, .admin-table td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--gridline); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text-primary);
  color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 13px; z-index: 100;
}
.toast.is-error { background: var(--status-critical); }

.hint { font-size: 12px; color: var(--text-muted); }
code.copy-code {
  display: block; background: var(--page-plane); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font-size: 12px; word-break: break-all;
}

/* Атрибут hidden должен побеждать авторские display-правила: .login-screen и .modal-backdrop
   задают display:flex, который иначе перекрывает браузерный display:none для [hidden]. */
[hidden] { display: none !important; }
