:root {
    color-scheme: light dark;
    --page: #f9f9f7;
    --surface: #ffffff;
    --surface-2: #f4f4f1;
    --border: #e4e3dd;
    --border-strong: #d3d2ca;
    --text: #1a1a19;
    --text-2: #56554f;
    --text-muted: #8b8983;
    --accent: #2a78d6;
    --accent-ink: #ffffff;
    --danger: #d03b3b;
    --danger-soft: #fbeaea;
    --good: #0ca30c;
    --warn: #e8a300;
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --radius: 12px;
    --c1:#2a78d6; --c2:#eb6834; --c3:#1baf7a; --c4:#eda100;
    --c5:#e87ba4; --c6:#008300; --c7:#4a3aa7; --c8:#e34948;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --page:#0f0f0e; --surface:#1a1a19; --surface-2:#232321;
      --border:#2f2f2c; --border-strong:#3c3c38;
      --text:#f4f4f0; --text-2:#c3c2b7; --text-muted:#8b8983;
      --accent:#3987e5; --danger:#e66767; --danger-soft:#3a2020;
      --good:#22b422; --warn:#f0b429;
      --shadow: 0 1px 3px rgba(0,0,0,0.4);
      --c1:#3987e5; --c2:#d95926; --c3:#199e70; --c4:#c98500;
      --c5:#d55181; --c6:#2a9d2a; --c7:#9085e9; --c8:#e66767;
    }
  }
  * { box-sizing: border-box; }
  body {
    margin:0; background:var(--page); color:var(--text);
    font-family: system-ui,-apple-system,"Segoe UI",sans-serif;
    font-size:14px; line-height:1.5;
  }
  .wrap { max-width:1160px; margin:0 auto; padding:20px 18px 60px; }
  header.app { display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap; margin-bottom:18px; }
  .brand { display:flex; align-items:center; gap:12px; }
  .brand .logo { width:40px; height:40px; border-radius:11px; background:linear-gradient(135deg,var(--accent),#1baf7a); display:flex; align-items:center; justify-content:center; font-size:22px; }
  .brand h1 { font-size:19px; margin:0; letter-spacing:-0.01em; }
  .brand p { margin:0; font-size:12.5px; color:var(--text-muted); }
  .toolbar { display:flex; align-items:center; gap:8px; flex-wrap:wrap; }

  select, input[type=text], input[type=number], input[type=date], input[type=password], textarea {
    font:inherit; color:var(--text); background:var(--surface);
    border:1px solid var(--border-strong); border-radius:9px; padding:9px 11px; width:100%;
  }
  select { appearance:none; background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238b8983' stroke-width='2.5'><path d='M6 9l6 6 6-6'/></svg>"); background-repeat:no-repeat; background-position:right 10px center; padding-right:30px; }
  input:focus, select:focus, textarea:focus { outline:2px solid var(--accent); outline-offset:-1px; border-color:var(--accent); }
  label.fld { display:block; font-size:12px; font-weight:600; color:var(--text-2); margin-bottom:5px; }

  button { font:inherit; cursor:pointer; border-radius:9px; border:1px solid transparent; padding:9px 14px; font-weight:600; transition:filter .12s, background .12s; }
  button:hover { filter:brightness(0.97); }
  .btn-primary { background:var(--accent); color:var(--accent-ink); }
  .btn-ghost { background:var(--surface); color:var(--text); border-color:var(--border-strong); }
  .btn-soft { background:var(--surface-2); color:var(--text); }
  .btn-danger { background:transparent; color:var(--danger); border-color:transparent; padding:6px 8px; }
  .btn-icon { background:transparent; border-color:transparent; padding:6px 8px; color:var(--text-2); }
  .btn-sm { padding:6px 10px; font-size:12.5px; }

  .card { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); box-shadow:var(--shadow); }
  .card .hd { padding:14px 16px 0; }
  .card .hd h2 { font-size:14.5px; margin:0 0 2px; }
  .card .hd p { margin:0; font-size:12px; color:var(--text-muted); }
  .card .bd { padding:16px; }

  .grid { display:grid; gap:16px; }
  .stats { grid-template-columns:repeat(4,1fr); }
  .stat { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); padding:14px 16px; box-shadow:var(--shadow); }
  .stat .lbl { font-size:12px; color:var(--text-muted); font-weight:600; }
  .stat .val { font-size:clamp(16px,4.6vw,22px); font-weight:700; letter-spacing:-0.02em; margin-top:3px; overflow-wrap:anywhere; }
  .stat .sub { font-size:11.5px; color:var(--text-muted); margin-top:2px; }

  .cols { display:grid; grid-template-columns:1.15fr 1fr; gap:16px; align-items:start; }

  table { width:100%; border-collapse:collapse; }
  th, td { text-align:left; padding:10px 10px; border-bottom:1px solid var(--border); font-size:13px; vertical-align:middle; }
  th { font-size:11.5px; text-transform:uppercase; letter-spacing:.04em; color:var(--text-muted); font-weight:700; }
  td.num, th.num { text-align:right; font-variant-numeric:tabular-nums; }
  tbody tr:hover { background:var(--surface-2); }
  th.sortable { cursor:pointer; user-select:none; white-space:nowrap; }
  th.sortable:hover { color:var(--text); }
  th .arw { opacity:.35; font-size:10px; margin-left:3px; }
  th.sorted .arw { opacity:1; color:var(--accent); }
  .chip { display:inline-flex; align-items:center; gap:6px; font-size:12px; font-weight:600; padding:3px 9px; border-radius:20px; background:var(--surface-2); }
  .chip .dot { width:9px; height:9px; border-radius:50%; flex:none; }
  .empty { text-align:center; color:var(--text-muted); padding:34px 10px; font-size:13px; }

  .donut-wrap { display:flex; gap:18px; align-items:center; flex-wrap:wrap; }
  .donut { position:relative; width:180px; height:180px; flex:none; }
  .donut .center { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center; text-align:center; }
  .donut .center .t { font-size:11px; color:var(--text-muted); font-weight:600; }
  .donut .center .v { font-size:16.5px; font-weight:700; letter-spacing:-0.01em; }
  .legend { flex:1; min-width:200px; display:flex; flex-direction:column; gap:2px; }
  .legend .row { display:flex; align-items:center; gap:9px; padding:6px 4px; border-radius:8px; }
  .legend .row:hover { background:var(--surface-2); }
  .legend .dot { width:11px; height:11px; border-radius:3px; flex:none; }
  .legend .nm { flex:1; font-size:13px; }
  .legend .pct { font-weight:700; font-size:13px; font-variant-numeric:tabular-nums; }
  .legend .amt { font-size:11.5px; color:var(--text-muted); font-variant-numeric:tabular-nums; min-width:96px; text-align:right; }

  .bud { display:flex; flex-direction:column; gap:14px; }
  .bud .item .top { display:flex; justify-content:space-between; align-items:baseline; margin-bottom:6px; gap:8px; }
  .bud .item .nm { font-weight:600; font-size:13px; display:flex; align-items:center; gap:7px; }
  .bud .item .fig { font-size:12px; color:var(--text-2); font-variant-numeric:tabular-nums; }
  .bar { height:9px; border-radius:6px; background:var(--surface-2); overflow:hidden; }
  .bar > span { display:block; height:100%; border-radius:6px; }
  .bud .over { color:var(--danger); font-weight:700; }

  .modal-bg { position:fixed; inset:0; background:rgba(0,0,0,.45); display:none; align-items:center; justify-content:center; padding:18px; z-index:50; }
  .modal-bg.on { display:flex; }
  .modal { background:var(--surface); border-radius:14px; width:100%; max-width:560px; max-height:90vh; overflow:auto; box-shadow:0 20px 50px rgba(0,0,0,.3); }
  .modal.narrow { max-width:400px; }
  .modal .mh { display:flex; justify-content:space-between; align-items:center; padding:16px 18px; border-bottom:1px solid var(--border); position:sticky; top:0; background:var(--surface); }
  .modal .mh h3 { margin:0; font-size:16px; }
  .modal .mb { padding:18px; }
  .modal .mf { padding:14px 18px; border-top:1px solid var(--border); display:flex; justify-content:flex-end; gap:8px; position:sticky; bottom:0; background:var(--surface); }
  .row2 { display:grid; grid-template-columns:1fr 1fr; gap:12px; }
  .mut { color:var(--text-muted); font-size:12px; }
  .hint { background:var(--surface-2); border-radius:9px; padding:10px 12px; font-size:12px; color:var(--text-2); }
  .toast { position:fixed; bottom:22px; left:50%; transform:translateX(-50%); background:var(--text); color:var(--page); padding:11px 18px; border-radius:10px; font-size:13px; font-weight:600; box-shadow:0 8px 24px rgba(0,0,0,.25); opacity:0; pointer-events:none; transition:opacity .2s, transform .2s; z-index:120; }
  .toast.on { opacity:1; transform:translateX(-50%) translateY(-4px); }
  .imp-table td, .imp-table th { padding:6px 8px; font-size:12px; }
  .imp-ok { color:var(--good); } .imp-bad { color:var(--danger); }
  .filter-inline { display:flex; gap:8px; align-items:center; flex-wrap:wrap; }
  .filter-inline select { width:auto; min-width:130px; max-width:100%; }
  .show-sm { display:none; }

  /* export dropdown */
  .dd { position:relative; }
  .dd-menu { position:absolute; right:0; top:calc(100% + 6px); background:var(--surface); border:1px solid var(--border-strong); border-radius:10px; box-shadow:0 10px 30px rgba(0,0,0,.18); padding:6px; min-width:170px; display:none; z-index:40; }
  .dd-menu.on { display:block; }
  .dd-menu button { display:flex; align-items:center; gap:9px; width:100%; text-align:left; background:transparent; border:none; padding:9px 10px; border-radius:8px; font-weight:600; font-size:13px; color:var(--text); }
  .dd-menu button:hover { background:var(--surface-2); }

  /* tabs in add modal */
  .tabs { display:flex; gap:4px; background:var(--surface-2); padding:4px; border-radius:10px; margin-bottom:16px; }
  .tabs button { flex:1; background:transparent; border:none; padding:8px; border-radius:7px; font-size:13px; color:var(--text-2); }
  .tabs button.active { background:var(--surface); color:var(--text); box-shadow:var(--shadow); }

  /* pagination */
  .pager { display:flex; align-items:center; justify-content:space-between; gap:10px; padding:12px 4px 2px; flex-wrap:wrap; }
  .pager .pg-btns { display:flex; gap:6px; align-items:center; }
  .pager .pg-btns button { padding:6px 11px; }
  .pager .pg-btns button:disabled { opacity:.4; cursor:default; }

  /* lock screen */
  .lock { position:fixed; inset:0; z-index:200; background:var(--page); display:flex; align-items:center; justify-content:center; padding:20px; }
  .lock .box { text-align:center; max-width:340px; width:100%; }
  .lock .logo { width:64px; height:64px; border-radius:18px; margin:0 auto 18px; background:linear-gradient(135deg,var(--accent),#1baf7a); display:flex; align-items:center; justify-content:center; font-size:34px; }
  .lock h2 { margin:0 0 4px; font-size:20px; }
  .lock p { margin:0 0 22px; color:var(--text-muted); font-size:13px; }
  .pin-dots { display:flex; gap:12px; justify-content:center; margin-bottom:22px; }
  .pin-dots .d { width:15px; height:15px; border-radius:50%; border:2px solid var(--border-strong); transition:.12s; }
  .pin-dots .d.fill { background:var(--accent); border-color:var(--accent); }
  .pin-err { color:var(--danger); font-size:13px; height:18px; margin-bottom:10px; font-weight:600; }
  .keypad { display:grid; grid-template-columns:repeat(3,1fr); gap:12px; max-width:260px; margin:0 auto; }
  .keypad button { aspect-ratio:1; font-size:22px; font-weight:600; border-radius:50%; background:var(--surface); border:1px solid var(--border); box-shadow:var(--shadow); }
  .keypad button.act { background:var(--surface-2); }
  .keypad .blank { visibility:hidden; }
  .lock.shake { animation:shake .4s; }
  @keyframes shake { 0%,100%{transform:translateX(0)} 20%,60%{transform:translateX(-8px)} 40%,80%{transform:translateX(8px)} }

  #printArea { display:none; }

  @media print {
    body { background:#fff; }
    .wrap, .modal-bg, .toast, .lock, .dd-menu { display:none !important; }
    #printArea { display:block !important; color:#000; padding:0; font-size:12px; }
    #printArea h1 { font-size:20px; margin:0 0 2px; }
    #printArea .meta { color:#555; margin-bottom:16px; font-size:12px; }
    #printArea table { width:100%; border-collapse:collapse; margin-bottom:18px; }
    #printArea th, #printArea td { border:1px solid #ccc; padding:6px 8px; text-align:left; font-size:11px; }
    #printArea td.num, #printArea th.num { text-align:right; }
    #printArea h3 { font-size:14px; margin:14px 0 6px; }
    #printArea .tot { font-weight:700; }
  }

  @media (max-width:900px){
    .stats{grid-template-columns:repeat(2,1fr);}
    .cols{grid-template-columns:1fr;}
  }
  @media (max-width:640px){
    header.app{flex-direction:column; align-items:stretch;}
    .toolbar{width:100%;}
    .toolbar > .filter-inline{flex:1 1 100%;}
    .toolbar > .filter-inline select{flex:1;}
    #btnAdd{flex:1 1 auto;}
    .dd{flex:1 1 auto;} .dd > #btnExport{width:100%;}
    .dd-menu{left:0; right:auto; width:100%;}
  }
  @media (max-width:520px){
    .stats{grid-template-columns:1fr 1fr;}
    .hide-sm{display:none !important;}
    .show-sm{display:flex !important;}
    .row2{grid-template-columns:1fr;}
    .wrap{padding:16px 13px 48px;}
    .card .bd{padding:13px;}
    th,td{padding:9px 7px; font-size:12.5px;}
    .legend .amt{min-width:auto;}
    .donut{width:150px;height:150px;}
    .donut svg{width:150px;height:150px;}
    .brand h1{font-size:17px;}
    .modal .mf{flex-wrap:wrap;}
    .modal .mf button{flex:1 1 auto;}
  }
  @media (max-width:360px){
    .stats{grid-template-columns:1fr;}
  }
