:root {
  --purple: #7c3aed;
  --purple-dark: #5b21b6;
  --teal: #14b8a6;
  --pink: #ec4899;
  --ink: #1e1b2e;
  --muted: #6b7280;
  --line: #e7e3f0;
  --bg: #f6f4fb;
  --card: #ffffff;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(30, 27, 46, .08), 0 6px 24px rgba(30, 27, 46, .06);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  padding-bottom: 84px;
}

/* Header */
.app-header {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px calc(14px + env(safe-area-inset-top, 0px));
  padding-top: max(14px, env(safe-area-inset-top, 14px));
  background: linear-gradient(120deg, var(--purple), var(--pink));
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 30px; line-height: 1; }
.brand h1 { font-size: 19px; margin: 0; letter-spacing: .3px; }
.brand p { font-size: 11px; margin: 2px 0 0; opacity: .85; text-transform: uppercase; letter-spacing: 1.2px; }
.pill {
  font-size: 11px; padding: 4px 10px; border-radius: 999px; font-weight: 600;
  background: rgba(255,255,255,.2);
}
.pill-ok { background: rgba(255,255,255,.92); color: var(--good); }
.pill-bad { background: #fff; color: var(--bad); }
.pill-muted { opacity: .8; }
.header-right { display: flex; align-items: center; gap: 8px; }
.auth-btn {
  width: auto; font-size: 11px; font-weight: 700; cursor: pointer;
  padding: 5px 11px; border-radius: 999px; border: 1px solid rgba(255,255,255,.55);
  background: rgba(255,255,255,.12); color: #fff; white-space: nowrap;
}
.auth-btn.out { background: #fff; color: var(--purple-dark); border-color: #fff; }

/* Tabbar — bottom on mobile; flex so it adapts to how many tabs are visible */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tab {
  flex: 1 1 0; min-width: 0;
  border: 0; background: none; cursor: pointer;
  padding: 9px 4px 10px; font-size: 11px; font-weight: 600;
  color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tab[hidden] { display: none; }
.tab span { font-size: 19px; }
.tab.is-active { color: var(--purple); }

/* Content */
.content { max-width: 680px; margin: 0 auto; padding: 18px 16px 24px; }
.view { display: none; animation: fade .18s ease; }
.view.is-active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.view-title { font-size: 17px; margin: 2px 0 16px; }

/* Segmented control */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
  background: #ece8f6; padding: 5px; border-radius: 12px; }
.seg-btn {
  border: 0; background: none; padding: 10px 6px; border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.seg-btn.is-active { background: var(--card); color: var(--purple); box-shadow: var(--shadow); }
.hint { font-size: 12.5px; color: var(--muted); margin: 10px 2px 4px; min-height: 16px; }

/* Form */
#entry-form { margin-top: 8px; }
.field { display: block; margin-bottom: 16px; }
.field > span:first-child { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field em { color: var(--muted); font-style: normal; font-weight: 400; }
.label-row { display: flex; align-items: center; justify-content: space-between; }
input, select, .mini-select {
  width: 100%; font-size: 15px; padding: 12px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: var(--card); color: var(--ink); font-family: inherit;
}
input:focus, select:focus { outline: none; border-color: var(--purple); box-shadow: 0 0 0 3px rgba(124,58,237,.15); }
.mini-select { width: auto; padding: 5px 8px; font-size: 12px; border-radius: 8px; }
#item-search { margin-bottom: 6px; }

.primary-btn {
  width: 100%; border: 0; cursor: pointer; color: #fff; font-size: 15px; font-weight: 700;
  padding: 14px; border-radius: 12px; margin-top: 4px;
  background: linear-gradient(120deg, var(--purple), var(--pink));
}
.primary-btn:disabled { opacity: .55; cursor: progress; }
.ghost-btn { width: auto; cursor: pointer; padding: 9px 14px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--ink); }
.form-msg { min-height: 20px; margin: 12px 2px 0; font-size: 13.5px; font-weight: 600; }
.form-msg.ok { color: var(--good); }
.form-msg.err { color: var(--bad); }

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar input[type="text"] { flex: 1 1 140px; }
.toolbar select { width: auto; flex: 0 0 auto; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.check input { width: auto; }

/* Table */
.table-wrap { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
.grid th, .grid td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--line); }
.grid th { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); background: #faf9fd; }
.grid tr:last-child td { border-bottom: 0; }
.grid .num { text-align: right; font-variant-numeric: tabular-nums; }
.grid .sku { font-size: 11px; color: var(--muted); }
.row-low { background: #fff7ed; }
.row-neg { background: #fef2f2; }
.stock-low { color: var(--warn); font-weight: 700; }
.stock-neg { color: var(--bad); font-weight: 700; }
.tag { font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: #ece8f6; color: var(--purple-dark); }

/* History feed */
.feed { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.feed li { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px; box-shadow: var(--shadow); }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; }
.dot.PRODUCTION { background: var(--good); }
.dot.CONSUMPTION { background: var(--bad); }
.dot.ADJUSTMENT { background: var(--warn); }
.feed .main { flex: 1 1 auto; min-width: 0; }
.feed .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.feed .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.feed .delta { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 14px; }
.delta.pos { color: var(--good); }
.delta.neg { color: var(--bad); }

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px; }
.card.soft h3 { margin: 0 0 6px; font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
p.muted { line-height: 1.5; }

/* Dashboard */
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.view-head .view-title { margin-bottom: 12px; }
.sub { font-size: 14px; margin: 22px 2px 10px; }
.sub:first-of-type { margin-top: 16px; }

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 6px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 13px 14px; box-shadow: var(--shadow); }
.kpi .v { font-size: 22px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi .l { font-size: 11.5px; color: var(--muted); margin-top: 3px; text-transform: uppercase; letter-spacing: .5px; }
.kpi.good .v { color: var(--good); }
.kpi.warn .v { color: var(--warn); }
.kpi.bad .v { color: var(--bad); }
@media (min-width: 560px) { .kpis { grid-template-columns: repeat(4, 1fr); } }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.scard { background: var(--card); border: 1px solid var(--line); border-left-width: 4px; border-radius: 12px;
  padding: 11px 13px; box-shadow: var(--shadow); }
.scard.ok { border-left-color: var(--good); }
.scard.low { border-left-color: var(--warn); background: #fffdf7; }
.scard.neg { border-left-color: var(--bad); background: #fff7f7; }
.scard .nm { font-size: 13px; font-weight: 600; line-height: 1.25; }
.scard .st { font-size: 20px; font-weight: 800; margin-top: 5px; font-variant-numeric: tabular-nums; }
.scard.low .st { color: var(--warn); }
.scard.neg .st { color: var(--bad); }
.scard .u { font-size: 11px; color: var(--muted); font-weight: 500; }
.empty-state { color: var(--muted); font-size: 13px; padding: 6px 2px; }

.chart-card { margin-top: 4px; }
.chart-box { position: relative; height: 240px; }
.chart-box.donut { height: 260px; }
.empty-note { text-align: center; margin: 8px 0 2px; }

/* Login modal */
.modal-overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(30, 27, 46, .5); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; padding: 18px;
}
.modal-overlay[hidden] { display: none; }
.modal {
  position: relative; width: 100%; max-width: 380px; background: var(--card);
  border-radius: var(--radius); padding: 22px 20px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal h3 { margin: 0 0 6px; font-size: 17px; }
.modal p { margin: 0 0 14px; line-height: 1.5; }
.modal form { margin-top: 4px; }
.modal-close {
  position: absolute; top: 8px; right: 10px; width: auto; border: 0; background: none;
  font-size: 26px; line-height: 1; color: var(--muted); cursor: pointer; padding: 4px 8px;
}

/* Planner */
.plan-inputs { display: grid; gap: 10px; }
.plan-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.plan-row label { font-size: 14px; font-weight: 600; }
.plan-row .sub-sku { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
.plan-row input { width: 96px; text-align: right; }
.plan-actions { display: flex; gap: 10px; margin-top: 14px; }
.primary-btn.inline { width: auto; flex: 1 1 auto; margin-top: 0; padding: 12px; font-size: 14px; }
.plan-actions .ghost-btn { flex: 0 0 auto; }

/* Wider screens */
@media (min-width: 720px) {
  .grid th, .grid td { padding: 12px 16px; }
}
