@layer reset, tokens, base, layout, components, views;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  body, h1, h2, h3, p, figure { margin: 0; }
  button, input, select, textarea { font: inherit; color: inherit; }
  table { border-collapse: collapse; }
}

@layer tokens {
  :root {
    color-scheme: light dark;
    --bg: light-dark(#f4f3ef, #14171a);
    --surface: light-dark(#ffffff, #1d2125);
    --surface-2: light-dark(#f8f7f4, #242a2f);
    --line: light-dark(#e3e1da, #323a41);
    --text: light-dark(#1b2024, #e9ecee);
    --muted: light-dark(#6a726f, #98a2a8);
    --accent: light-dark(#0f766e, #2dd4bf);
    --accent-ink: light-dark(#ffffff, #06231f);
    --accent-soft: light-dark(#e1f1ee, #173b38);
    --good: light-dark(#15803d, #4ade80);
    --bad: light-dark(#b42318, #f87171);
    --warn: light-dark(#b45309, #fbbf24);
    --c1: light-dark(#0f766e, #2dd4bf);
    --c2: light-dark(#c2410c, #fb923c);
    --c3: light-dark(#4f46e5, #a5b4fc);
    --c4: light-dark(#a21caf, #f0abfc);
    --c5: light-dark(#4d7c0f, #bef264);
    --radius: 14px;
    --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 14px rgb(0 0 0 / .05);
    --nav-w: 232px;
    --tabbar-h: 64px;
    --safe-b: env(safe-area-inset-bottom, 0px);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  }
  :root[data-theme="light"] { color-scheme: light; }
  :root[data-theme="dark"] { color-scheme: dark; }
}

@layer base {
  html { -webkit-text-size-adjust: 100%; }
  body { background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.45; min-height: 100dvh; font-variant-numeric: tabular-nums; }
  a { color: var(--accent); }
  h1 { font-size: 1.45rem; letter-spacing: -.01em; }
  h2 { font-size: 1.05rem; }
  .muted { color: var(--muted); }
  .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .good { color: var(--good); } .bad { color: var(--bad); }
  .boot { padding: 3rem; text-align: center; color: var(--muted); }
  :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  ::selection { background: var(--accent-soft); }
}

@layer layout {
  .shell { display: grid; grid-template-columns: var(--nav-w) minmax(0, 1fr); min-height: 100dvh; }
  .nav { position: sticky; top: 0; height: 100dvh; padding: 1.1rem .8rem; display: flex; flex-direction: column; gap: .2rem; border-right: 1px solid var(--line); background: var(--surface); overflow-y: auto; }
  .brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; padding: .2rem .6rem 1rem; }
  .brand img { width: 30px; height: 30px; border-radius: 8px; }
  .nav a.item { display: flex; align-items: center; gap: .7rem; padding: .6rem .7rem; border-radius: 10px; color: var(--text); text-decoration: none; }
  .nav a.item:hover { background: var(--surface-2); }
  .nav a.item[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
  .nav .sp { flex: 1; }
  .nav .who { font-size: .85rem; color: var(--muted); padding: .6rem; border-top: 1px solid var(--line); margin-top: .5rem; }
  .ico { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  main { padding: 1.4rem clamp(.9rem, 3vw, 2.2rem) 3rem; min-width: 0; container-type: inline-size; }
  .pagehead { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem 1rem; margin-bottom: 1.1rem; }
  .pagehead h1 { margin-right: auto; }
  .stack { display: grid; gap: 1.1rem; }

  @media (max-width: 860px) {
    .shell { grid-template-columns: 1fr; }
    .nav { position: fixed; inset: auto 0 0 0; height: calc(var(--tabbar-h) + var(--safe-b)); padding: .3rem .4rem calc(.3rem + var(--safe-b)); flex-direction: row; justify-content: flex-start; gap: 0; border: 0; border-top: 1px solid var(--line); z-index: 20; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
    .brand, .nav .sp, .nav .who { display: none; }
    .nav a.item { flex-direction: column; gap: .15rem; font-size: .68rem; padding: .35rem .3rem; min-width: 74px; flex: 1 0 auto; text-align: center; justify-content: center; white-space: nowrap; }
    main { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 2rem); }
  }
}

@layer components {
  .card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; }
  .card > h2 { margin-bottom: .7rem; }
  .btn { display: inline-flex; align-items: center; justify-content: center; gap: .45rem; min-height: 40px; padding: 0 .95rem; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; text-decoration: none; color: var(--text); white-space: nowrap; }
  .btn:hover { background: var(--surface-2); }
  .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
  .btn.primary:hover { filter: brightness(1.08); }
  .btn.danger { color: var(--bad); }
  .btn.icon { padding: 0; width: 40px; }
  .btn.sm { min-height: 32px; padding: 0 .65rem; font-size: .88rem; }
  .btn[disabled] { opacity: .5; cursor: not-allowed; }
  .seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
  .seg > button, .seg > a { border: 0; background: none; padding: 0 .8rem; min-height: 38px; cursor: pointer; color: var(--text); text-decoration: none; display: inline-flex; align-items: center; }
  .seg > [aria-pressed="true"], .seg > [aria-current="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
  .tabs { display: flex; gap: .4rem; overflow-x: auto; padding-bottom: .2rem; scrollbar-width: thin; }
  .tab { padding: .45rem .95rem; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); text-decoration: none; color: var(--text); white-space: nowrap; }
  .tab[aria-current="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
  .tab.off { opacity: .6; }

  input, select, textarea { width: 100%; min-height: 38px; padding: .35rem .6rem; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); }
  input[type="checkbox"] { width: 18px; min-height: 0; height: 18px; accent-color: var(--accent); }
  input:read-only:not([type="checkbox"]) { background: var(--surface-2); }
  input[type="number"] { appearance: textfield; } 
  textarea { min-height: 70px; resize: vertical; }
  label.f { display: grid; gap: .25rem; font-size: .78rem; color: var(--muted); }
  label.f > span { text-transform: uppercase; letter-spacing: .03em; font-size: .68rem; }
  label.f input, label.f select, label.f textarea { color: var(--text); font-size: .95rem; }
  .ro { background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; min-height: 38px; padding: .45rem .6rem; font-weight: 600; text-align: right; }

  .kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .8rem; }
  .kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .85rem 1rem; box-shadow: var(--shadow); }
  .kpi small { color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-size: .68rem; }
  .kpi b { display: block; font-size: 1.5rem; letter-spacing: -.02em; margin-top: .15rem; }
  .kpi .sub { font-size: .82rem; color: var(--muted); }
  .bar { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin-top: .5rem; }
  .bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }

  .tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  table.t { width: 100%; font-size: .9rem; }
  table.t th, table.t td { padding: .5rem .7rem; border-bottom: 1px solid var(--line); text-align: left; }
  table.t th { font-size: .72rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
  table.t td.num, table.t th.num { text-align: right; }
  table.t tr:last-child td { border-bottom: 0; }
  table.t tfoot td { font-weight: 700; background: var(--surface-2); }
  table.t td input { min-height: 30px; padding: .15rem .4rem; border-color: transparent; background: transparent; text-align: right; width: 100%; min-width: 5.5rem; }
  table.t td input:hover { border-color: var(--line); } table.t td input:focus { background: var(--surface); }
  table.t .sticky { position: sticky; left: 0; background: var(--surface); z-index: 1; font-weight: 600; }
  table.t tfoot .sticky, table.t th.sticky { background: var(--surface-2); }
  td.live { color: var(--accent); }

  .toasts, #toasts { position: fixed; right: 1rem; bottom: calc(var(--tabbar-h) + var(--safe-b) + .6rem); display: grid; gap: .5rem; z-index: 60; }
  @media (min-width: 861px) { #toasts { bottom: 1rem; } }
  .toast { background: var(--text); color: var(--bg); padding: .65rem .95rem; border-radius: 10px; box-shadow: var(--shadow); max-width: min(92vw, 380px); animation: pop .2s ease-out; }
  .toast.err { background: var(--bad); color: #fff; }
  @keyframes pop { from { transform: translateY(8px); opacity: 0; } }
  .savestate { font-size: .82rem; color: var(--muted); display: inline-flex; align-items: center; gap: .35rem; }
  .savestate::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
  .savestate[data-s="saving"]::before { background: var(--warn); } .savestate[data-s="error"]::before { background: var(--bad); }

  dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 1.2rem; max-width: min(92vw, 460px); width: 100%; box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
  dialog::backdrop { background: rgb(0 0 0 / .45); backdrop-filter: blur(2px); }
  dialog h2 { margin-bottom: .6rem; } dialog .row { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; }

  .chart { width: 100%; height: auto; display: block; }
  .chart text { fill: var(--muted); font-size: 11px; }
  .chart .grid { stroke: var(--line); } .chart .tick { fill: var(--muted); }
  .legend { display: flex; flex-wrap: wrap; gap: .4rem 1rem; font-size: .85rem; margin-top: .4rem; }
  .legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .35rem; }

  .empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
  .auth { min-height: 100dvh; display: grid; place-items: center; padding: 1rem; }
  .auth .card { width: min(100%, 420px); display: grid; gap: .9rem; padding: 1.5rem; }
  .auth h1 { display: flex; align-items: center; gap: .7rem; } .auth h1 img { width: 38px; border-radius: 10px; }
  .err { color: var(--bad); font-size: .9rem; min-height: 1.2em; }
  .hint { font-size: .85rem; color: var(--muted); }
}

@layer views {
  /* ---- заказы ---- */
  .order { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
  .order > summary { list-style: none; cursor: pointer; display: grid; grid-template-columns: 2rem 6.2rem minmax(0, 1.1fr) minmax(0, 1.4fr) auto 1.2rem; align-items: center; gap: .5rem .8rem; padding: .7rem .9rem; }
  .order > summary::-webkit-details-marker { display: none; }
  .order > summary .no { color: var(--muted); font-weight: 700; }
  .order > summary .ttl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .order > summary .sum { font-weight: 700; text-align: right; }
  .order > summary .chev { transition: transform .15s; color: var(--muted); }
  .order[open] > summary .chev { transform: rotate(90deg); }
  .order[open] > summary { border-bottom: 1px solid var(--line); }
  .order .body { padding: .9rem; display: grid; gap: .8rem; container-type: inline-size; }
  .grid-f { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; }
  .grid-f .wide { grid-column: 1 / -1; }
  .cats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .7rem; padding: .8rem; border-radius: 12px; background: var(--surface-2); border: 1px dashed var(--line); }
  .order .foot { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; }
  @media (max-width: 640px) {
    .order > summary { grid-template-columns: 1.6rem minmax(0, 1fr) auto 1rem; }
    .order > summary .dt, .order > summary .ttl.num2 { display: none; }
    .order > summary .ttl { grid-column: 2; }
  }
  .paygrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.1rem; align-items: start; }
  .line { display: flex; justify-content: space-between; gap: 1rem; padding: .42rem 0; border-bottom: 1px dashed var(--line); }
  .line:last-child { border-bottom: 0; } .line.total { font-weight: 700; font-size: 1.05rem; border-top: 2px solid var(--line); margin-top: .3rem; padding-top: .6rem; }
  .line input { width: 9rem; text-align: right; }

  /* ---- график ---- */
  .sched { max-height: 72dvh; }
  .sched table { font-size: .82rem; }
  .sched th, .sched td { padding: 0; text-align: center; min-width: 38px; border: 1px solid var(--line); }
  .sched th { padding: .3rem 0; line-height: 1.15; } .sched th small { display: block; font-weight: 400; text-transform: none; }
  .sched th.we, .sched td.we { background: light-dark(#fbefe9, #3a2a24); }
  .sched th.name, .sched td.name { min-width: 6.4rem; text-align: left; padding: 0 .6rem; position: sticky; left: 0; background: var(--surface); z-index: 2; font-weight: 600; }
  .sched td.name { box-shadow: 1px 0 0 var(--line); }
  .sched td.sum { min-width: 4rem; font-weight: 700; background: var(--surface-2); }
  .cell { width: 100%; height: 40px; border: 0; background: none; cursor: pointer; font-weight: 600; touch-action: manipulation; }
  .cell:hover { background: var(--accent-soft); }
  .cell.h { background: var(--accent-soft); color: var(--accent); } .cell.x { background: light-dark(#eceae4, #2d3338); color: var(--muted); } .cell.s { background: light-dark(#fff4d6, #4a3d12); color: var(--warn); }
  .brush { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
  .brush .seg button { min-width: 3.3rem; justify-content: center; }
  .brush input { width: 4.5rem; }

  .grid2 { align-items: start; display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 1.1rem; }
  .storage-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .6rem; padding: .7rem 0; border-bottom: 1px dashed var(--line); align-items: end; }
  @media print { .nav, .pagehead .btn, #toasts, .brush { display: none !important; } .shell { display: block; } body { background: #fff; } }
}
