/* NAS-Expert CRM - styles */
:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1a2433;
  --muted: #667388;
  --line: #dfe4ec;
  --accent: #0b4f8a;
  --accent-hover: #093f6f;
  --accent-soft: #e6f0fa;
  --on-accent: #ffffff;
  --danger: #c0392b;
  --danger-soft: #fbeae8;
  --ok: #1f8a4c;
  --warn-soft: #fff4d6;
  --shadow: 0 8px 28px rgba(15, 30, 55, .18);
  --radius: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12171f;
    --panel: #1b222d;
    --text: #e6ebf3;
    --muted: #93a0b4;
    --line: #2c3645;
    --accent: #4a9be0;
    --accent-hover: #6aaee8;
    --accent-soft: #1f3248;
    --on-accent: #08121f;
    --danger: #ef6a5b;
    --danger-soft: #3a1f1d;
    --ok: #4cd37b;
    --warn-soft: #3a3118;
    --shadow: 0 8px 28px rgba(0, 0, 0, .55);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}
h1, h2 { margin: 0; line-height: 1.2; }

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand img { border-radius: 8px; }
.brand h1 { font-size: 1.2rem; }
.sub { margin: 2px 0 0; font-size: .8rem; color: var(--muted); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; }

main { padding: 16px max(16px, env(safe-area-inset-right)) 32px max(16px, env(safe-area-inset-left)); max-width: 1800px; margin: 0 auto; }

/* ------------------------------------------------------------ buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .9rem;
  padding: 8px 14px; border-radius: 8px; cursor: pointer; text-decoration: none;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .55; cursor: progress; }
.btn:focus-visible, .icon-btn:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; padding: 0; border-radius: 8px;
  border: 1px solid transparent; background: transparent; color: var(--muted);
  cursor: pointer; text-decoration: none; flex: 0 0 auto;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
.icon { display: inline-flex; width: 18px; height: 18px; }
.icon svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.spin .icon svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------ banner / filters */
.banner { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 12px; background: var(--warn-soft); border: 1px solid var(--line); }
.banner.error { background: var(--danger-soft); border-color: var(--danger); color: var(--text); }

.filters {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px;
  margin-bottom: 8px;
}
.filter { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.filter > span { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.count { font-size: .82rem; color: var(--muted); margin: 6px 2px 8px; display: flex; gap: 12px; align-items: center; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font: inherit; text-decoration: underline; }

/* ------------------------------------------------------------ table */
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
table { border-collapse: collapse; width: 100%; min-width: 1100px; }
th { text-align: left; font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 8px; border-bottom: 1px solid var(--line); background: var(--panel); vertical-align: bottom; }
td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.c-customer { min-width: 140px; }
.c-model { min-width: 110px; }
.c-disks { min-width: 120px; }
.c-purchased { min-width: 70px; }
.c-invoice_date, .c-supplier_purchase_date { min-width: 108px; }
.c-supplier { min-width: 120px; }
.c-supplier_invoice { min-width: 110px; }
.c-docs { min-width: 150px; }
.c-comments { min-width: 130px; }
.c-links { min-width: 76px; }
.c-actions { min-width: 76px; }
.c-model .cv { flex-direction: column; gap: 0; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
td[data-edit] { cursor: text; }
td[data-edit]:hover .cv { text-decoration: underline dotted var(--muted); text-underline-offset: 3px; }
td.editing { padding: 4px 8px; background: var(--accent-soft); }
.cv { display: flex; align-items: flex-start; gap: 6px; min-width: 0; overflow-wrap: break-word; white-space: pre-wrap; }
.muted { color: var(--muted); }
.cv small { color: var(--muted); }
.docs-text { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-top: 5px; }
.cell-actions .cv, .cell-links .cv { gap: 2px; white-space: nowrap; }
td.empty { text-align: center; color: var(--muted); padding: 28px; }
.tag-yes { color: var(--ok); font-weight: 600; }

/* ------------------------------------------------------------ inputs */
input[type=text], input[type=password], input[type=date], textarea, select {
  width: 100%; font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px;
}
textarea { resize: vertical; min-height: 60px; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
td input[type=checkbox] { margin-top: 5px; cursor: pointer; }

/* combo box (typeable dropdown) */
.combo { position: relative; width: 100%; }
.combo-input { padding-right: 30px; }
.combo-clear {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; border: 0; border-radius: 6px; background: transparent;
  color: var(--muted); font-size: 1.1rem; line-height: 1; cursor: pointer; display: none;
}
.combo.has-value.clearable .combo-clear { display: block; }
.combo-clear:hover { background: var(--accent-soft); color: var(--text); }
.combo-list {
  position: fixed; z-index: 1000; margin: 4px 0; padding: 4px; list-style: none;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); overflow-y: auto; overscroll-behavior: contain;
}
.combo-item { display: flex; justify-content: space-between; gap: 12px; padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.combo-item .sub { color: var(--muted); font-size: .8rem; white-space: nowrap; margin: 0; }
.combo-item.active { background: var(--accent-soft); }
.combo-item.selected .lbl { font-weight: 700; }
.combo-item.create { color: var(--accent); font-weight: 600; }
.combo-empty { padding: 8px 10px; color: var(--muted); }

/* ------------------------------------------------------------ modal */
.modal {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  margin: auto; padding: 0; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
  overflow: hidden;
}
dialog.modal[open] { display: flex; flex-direction: column; }
dialog.modal::backdrop { background: rgba(10, 18, 30, .55); }
.modal.wide { width: min(720px, calc(100vw - 24px)); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.modal-head h2 { font-size: 1.05rem; }
.modal-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span, .field > label { font-size: .8rem; font-weight: 600; color: var(--muted); }
.field .hint { font-weight: 400; font-size: .75rem; }
.req { color: var(--danger); }
.check { display: flex; align-items: center; gap: 10px; font-weight: 600; cursor: pointer; }
.extra { border: 1px dashed var(--line); border-radius: var(--radius); padding: 12px; margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.extra legend { padding: 0 6px; font-size: .8rem; font-weight: 600; color: var(--muted); }
.extra .full { grid-column: 1 / -1; }
.form-error { background: var(--danger-soft); border: 1px solid var(--danger); padding: 8px 12px; border-radius: 8px; }

.supplier-row { display: flex; gap: 6px; align-items: center; }
.supplier-row .used { font-size: .75rem; color: var(--muted); white-space: nowrap; min-width: 70px; text-align: right; }
.supplier-add { display: flex; gap: 8px; }

/* ------------------------------------------------------------ toasts */
.toasts { position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 3000; align-items: center; pointer-events: none; }
.toast { background: #1f2a3a; color: #fff; padding: 9px 16px; border-radius: 999px; font-size: .88rem; box-shadow: var(--shadow); max-width: 90vw; }
.toast.error { background: var(--danger); }

/* ------------------------------------------------------------ login */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 16px; }
.login-card { width: min(400px, 100%); display: flex; flex-direction: column; }
.login-brand { display: flex; align-items: center; gap: 12px; font-size: 1.1rem; }
.login-brand img { border-radius: 10px; }
.hint-text { margin: 0; color: var(--muted); font-size: .9rem; }
.login-card.wide-card { width: min(560px, 100%); }
.migration-list { margin: 0; padding-left: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.migration-list li { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; }
.migration-list li code { font-size: .8rem; color: var(--muted); }
.migration-list li.done { border-color: var(--ok); }
.migration-list li.done code::after { content: '  ✓ applied'; color: var(--ok); font-weight: 600; }

/* ------------------------------------------------------------ mobile */
@media (max-width: 860px) {
  .toolbar { width: 100%; }
  .toolbar .btn { flex: 1 1 auto; justify-content: center; }
  .table-wrap { background: transparent; border: 0; overflow: visible; }
  table, thead, tbody, tr, td { display: block; min-width: 0; width: 100%; }
  thead { display: none; }
  tbody tr { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; padding: 6px 8px; }
  tbody tr:hover { background: var(--panel); }
  td { display: flex; gap: 10px; border: 0; padding: 6px 4px; }
  td::before { content: attr(data-label); flex: 0 0 34%; max-width: 34%; font-size: .75rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding-top: 6px; }
  td.cell-actions::before, td.cell-links::before { padding-top: 6px; }
  .c-model .cv { flex-direction: row; gap: 8px; align-items: baseline; }
  td > .cv, td > .combo, td > input, td > textarea { flex: 1 1 auto; min-width: 0; }
  td.empty { display: block; }
  td.empty::before { content: none; }
  .docs-text { max-width: none; white-space: normal; }
  .extra { grid-template-columns: 1fr; }
  .modal { border-radius: 12px; }
}

@media (prefers-reduced-motion: reduce) { .spin .icon svg { animation: none; } }
