:root {
  color-scheme: light;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #eef2f7;
  --text: #111827;
  --muted: #64748b;
  --line: #d7dee8;
  --line-strong: #b8c2d0;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --accent-soft: #dbeafe;
  --success: #166534;
  --warning: #92400e;
  --danger: #991b1b;
  --focus: rgb(37 99 235 / 0.28);
  --shadow: 0 14px 40px rgb(15 23 42 / 0.08);
  --shadow-raised: 0 18px 48px rgb(15 23 42 / 0.14);
  --radius: 16px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="light"] { color-scheme: light; }

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #090e19;
  --surface: #111827;
  --surface-2: #172033;
  --surface-3: #202b40;
  --text: #f3f4f6;
  --muted: #a7b0c0;
  --line: #303b50;
  --line-strong: #46536a;
  --accent: #60a5fa;
  --accent-strong: #3b82f6;
  --accent-soft: #172b4d;
  --success: #86efac;
  --warning: #fcd34d;
  --danger: #fca5a5;
  --focus: rgb(96 165 250 / 0.34);
  --shadow: 0 14px 40px rgb(0 0 0 / 0.24);
  --shadow-raised: 0 20px 56px rgb(0 0 0 / 0.38);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #090e19;
    --surface: #111827;
    --surface-2: #172033;
    --surface-3: #202b40;
    --text: #f3f4f6;
    --muted: #a7b0c0;
    --line: #303b50;
    --line-strong: #46536a;
    --accent: #60a5fa;
    --accent-strong: #3b82f6;
    --accent-soft: #172b4d;
    --success: #86efac;
    --warning: #fcd34d;
    --danger: #fca5a5;
    --focus: rgb(96 165 250 / 0.34);
    --shadow: 0 14px 40px rgb(0 0 0 / 0.24);
    --shadow-raised: 0 20px 56px rgb(0 0 0 / 0.38);
  }
}

* { box-sizing: border-box; }
html { min-width: 320px; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; }
button, select { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.48; }
input, select, textarea {
  width: 100%;
  min-height: 2.75rem;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 0.68rem 0.78rem;
  background: var(--surface);
  color: var(--text);
}
input:hover, select:hover, textarea:hover { border-color: var(--line-strong); }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-color: var(--accent);
}
textarea { resize: vertical; }
[hidden] { display: none !important; }

.skip-link {
  position: fixed;
  left: 1rem;
  top: -5rem;
  z-index: 100;
  background: var(--text);
  color: var(--surface);
  padding: 0.7rem 1rem;
  border-radius: 10px;
}
.skip-link:focus { top: 1rem; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(200px, 1fr) auto minmax(300px, 1fr);
  align-items: center;
  gap: 1rem;
  padding: 0.8rem max(1rem, env(safe-area-inset-right)) 0.8rem max(1rem, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 91%, transparent);
  box-shadow: 0 1px 0 rgb(15 23 42 / 0.02);
  backdrop-filter: blur(18px) saturate(130%);
}
.brand-row { display: flex; align-items: center; gap: 0.75rem; min-width: 0; }
.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 2.45rem;
  height: 2.45rem;
  border-radius: 12px;
  background: var(--text);
  color: var(--surface);
  font-weight: 900;
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 0.08);
}
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 1.18rem; letter-spacing: -0.02em; }
h2 { margin-bottom: 0; font-size: clamp(1.35rem, 2vw, 1.95rem); letter-spacing: -0.025em; }
h3 { margin-bottom: 0.5rem; }
.eyebrow {
  margin-bottom: 0.15rem;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}

.nav-tabs {
  display: flex;
  padding: 0.25rem;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--surface-2);
}
.nav-tab {
  min-height: 2.45rem;
  border: 0;
  border-radius: 9px;
  padding: 0.52rem 0.88rem;
  background: transparent;
  color: var(--muted);
  font-weight: 760;
  white-space: nowrap;
}
.nav-tab.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 7px rgb(15 23 42 / 0.11);
}
.header-tools {
  justify-self: end;
  display: grid;
  grid-template-columns: minmax(105px, 0.55fr) minmax(165px, 1fr);
  align-items: end;
  gap: 0.65rem;
  width: min(100%, 390px);
}
.theme-field, .operator-field {
  display: grid;
  gap: 0.24rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 760;
}
.theme-field select, .operator-field input { min-height: 2.35rem; padding: 0.46rem 0.62rem; }

.shell {
  width: min(1440px, calc(100% - 2rem));
  margin: 0 auto;
  padding: clamp(1rem, 2.5vw, 2rem) 0 max(4rem, env(safe-area-inset-bottom));
}
.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.85rem;
  margin-bottom: 1rem;
}
.metric, .panel, .security-note {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.metric {
  position: relative;
  overflow: hidden;
  padding: 1rem 1.05rem;
}
.metric::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 2px;
  background: var(--accent);
  opacity: 0;
  transform: scaleX(0.25);
}
.metric span { display: block; margin-bottom: 0.22rem; color: var(--muted); font-size: 0.78rem; font-weight: 720; }
.metric strong { display: block; font-size: clamp(1.65rem, 4vw, 2.45rem); font-variant-numeric: tabular-nums; letter-spacing: -0.04em; }

.view {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: clip;
}
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.15rem 1.3rem;
  border-bottom: 1px solid var(--line);
}
.toolbar {
  display: grid;
  grid-template-columns: minmax(250px, 1fr) repeat(3, minmax(130px, 0.3fr));
  gap: 0.65rem;
  padding: 0.85rem 1.3rem;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
}
.table-wrap { overflow-x: auto; overscroll-behavior-x: contain; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.85rem 0.95rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { color: var(--muted); font-size: 0.72rem; letter-spacing: 0.055em; text-transform: uppercase; }
tbody tr:last-child td { border-bottom: 0; }
.inventory-row { background: var(--surface); }
.item-title { display: flex; align-items: center; gap: 0.72rem; min-width: 220px; }
.item-avatar {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 2.45rem;
  height: 2.45rem;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 900;
}
.item-title strong, .item-title small { display: block; }
.item-title small, .muted { color: var(--muted); }
.item-title small { margin-top: 0.12rem; }
.quantity { font-variant-numeric: tabular-nums; }
.quantity small { display: block; margin-top: 0.12rem; }
.actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 0.4rem; min-width: 230px; }

.button, .icon-button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-weight: 760;
  -webkit-tap-highlight-color: transparent;
}
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 2.75rem;
  padding: 0.56rem 0.82rem;
  border-radius: 11px;
  text-decoration: none;
}
.button:hover:not(:disabled) { border-color: var(--line-strong); background: var(--surface-2); }
.button:active:not(:disabled), .icon-button:active:not(:disabled), .nav-tab:active { transform: scale(0.97); }
.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  box-shadow: 0 6px 16px rgb(37 99 235 / 0.18);
}
.button.primary:hover:not(:disabled) { border-color: var(--accent-strong); background: var(--accent-strong); }
html[data-theme="dark"] .button.primary { color: #08111f; }
@media (prefers-color-scheme: dark) { :root:not([data-theme]) .button.primary { color: #08111f; } }
.button.danger { color: var(--danger); }
.button.compact { min-height: 2.5rem; padding: 0.4rem 0.62rem; font-size: 0.8rem; }
.icon-button { width: 2.75rem; height: 2.75rem; border-radius: 11px; font-size: 1.35rem; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 1.8rem;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.25rem 0.56rem;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
  white-space: nowrap;
}
.badge.available { background: color-mix(in srgb, #22c55e 14%, var(--surface)); color: var(--success); border-color: color-mix(in srgb, #22c55e 24%, transparent); }
.badge.low { background: color-mix(in srgb, #f59e0b 17%, var(--surface)); color: var(--warning); border-color: color-mix(in srgb, #f59e0b 28%, transparent); }
.badge.out, .badge.archived { background: color-mix(in srgb, #ef4444 13%, var(--surface)); color: var(--danger); border-color: color-mix(in srgb, #ef4444 24%, transparent); }
.empty-state { padding: clamp(2.5rem, 8vw, 4rem) 1.5rem; text-align: center; color: var(--muted); }
.empty-state h3 { color: var(--text); }

.activity-list { display: grid; }
.activity-entry {
  display: grid;
  grid-template-columns: 9rem minmax(160px, 0.72fr) minmax(210px, 1fr) auto;
  gap: 0.9rem;
  align-items: center;
  padding: 0.95rem 1.3rem;
  border-bottom: 1px solid var(--line);
}
.activity-entry:last-child { border-bottom: 0; }
.activity-entry time, .activity-entry small { color: var(--muted); font-size: 0.78rem; }
.activity-entry strong { display: block; }
.data-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.85rem; padding: 1.2rem; }
.panel { padding: 1.15rem; box-shadow: none; }
.panel p { color: var(--muted); line-height: 1.55; }
.file-button input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.security-note { margin: 0 1.2rem 1.2rem; padding: 1rem 1.05rem; box-shadow: none; line-height: 1.5; }

::backdrop { background: rgb(15 23 42 / 0.56); backdrop-filter: blur(5px); }
dialog {
  width: min(680px, calc(100% - 2rem));
  max-height: calc(100dvh - 2rem);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 30px 80px rgb(15 23 42 / 0.32);
}
dialog form { padding: 1.3rem; }
.dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.1rem; }
.dialog-context { padding: 0.78rem 0.88rem; border: 1px solid var(--line); border-radius: 11px; background: var(--surface-2); color: var(--muted); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.9rem; }
.form-grid label { display: grid; gap: 0.38rem; color: var(--muted); font-size: 0.8rem; font-weight: 750; }
.form-grid .wide { grid-column: 1 / -1; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.2rem; }
.form-error { margin: 1rem 0 0; color: var(--danger); font-weight: 700; }
.toast {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 50;
  max-width: min(420px, calc(100vw - 2rem));
  border: 1px solid color-mix(in srgb, var(--text) 16%, var(--line));
  border-radius: 13px;
  padding: 0.85rem 1rem;
  background: var(--text);
  color: var(--surface);
  box-shadow: var(--shadow-raised);
  transform: translateY(0.75rem) scale(0.98);
  opacity: 0;
}
.toast.is-visible { transform: none; opacity: 1; }

@media (hover: hover) {
  .metric:hover, .panel:hover { transform: translateY(-2px); border-color: var(--line-strong); box-shadow: var(--shadow-raised); }
  .inventory-row:hover { background: color-mix(in srgb, var(--accent) 3.5%, var(--surface)); }
  .nav-tab:hover:not(.is-active) { color: var(--text); background: color-mix(in srgb, var(--surface) 72%, transparent); }
}

@media (max-width: 1080px) {
  .topbar { grid-template-columns: minmax(190px, 1fr) minmax(300px, 1fr); }
  .nav-tabs { grid-row: 2; grid-column: 1 / -1; justify-self: stretch; }
  .nav-tab { flex: 1; }
  .header-tools { width: 100%; }
  .toolbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .search-field { grid-column: 1 / -1; }
  .activity-entry { grid-template-columns: 7rem 1fr auto; }
  .activity-entry > :nth-child(3) { grid-column: 2 / -1; }
}

@media (max-width: 760px) {
  .topbar { position: sticky; gap: 0.7rem; padding-top: max(0.7rem, env(safe-area-inset-top)); }
  .shell { width: min(100% - 1rem, 1440px); padding-top: 0.7rem; }
  .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.5rem; margin-bottom: 0.7rem; }
  .metric { padding: 0.78rem 0.82rem; border-radius: 14px; }
  .view { border-radius: 16px; }
  .section-heading { padding: 0.95rem 1rem; }
  .toolbar { grid-template-columns: 1fr; gap: 0.5rem; padding: 0.75rem; }
  .search-field { grid-column: auto; }
  .table-wrap { overflow: visible; padding: 0.65rem; background: var(--surface-2); }
  table, tbody { display: block; width: 100%; }
  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  tbody { display: grid; gap: 0.65rem; }
  .inventory-row {
    display: block;
    border: 1px solid var(--line);
    border-radius: 15px;
    overflow: hidden;
    box-shadow: 0 4px 18px rgb(15 23 42 / 0.045);
  }
  .inventory-row td {
    display: grid;
    grid-template-columns: minmax(6.5rem, 0.34fr) minmax(0, 1fr);
    align-items: center;
    gap: 0.7rem;
    width: 100%;
    padding: 0.72rem 0.82rem;
    border-bottom: 1px solid var(--line);
  }
  .inventory-row td::before { content: attr(data-label); color: var(--muted); font-size: 0.69rem; font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase; }
  .inventory-row td:first-child { display: block; padding: 0.9rem 0.82rem; }
  .inventory-row td:first-child::before, .inventory-row td:last-child::before { display: none; }
  .inventory-row td:last-child { display: block; border-bottom: 0; padding: 0.72rem 0.82rem 0.82rem; }
  .item-title { min-width: 0; }
  .actions { min-width: 0; justify-content: stretch; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .actions .button { width: 100%; min-height: 2.75rem; }
  .activity-entry { grid-template-columns: 1fr auto; gap: 0.38rem 0.8rem; padding: 0.85rem 1rem; }
  .activity-entry > :nth-child(2), .activity-entry > :nth-child(3) { grid-column: 1 / -1; }
  .activity-entry > :nth-child(4) { grid-column: 2; grid-row: 1; }
  .data-grid { grid-template-columns: 1fr; padding: 0.8rem; }
  .security-note { margin: 0 0.8rem 0.8rem; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .wide { grid-column: auto; }
}

@media (max-width: 560px) {
  .topbar { grid-template-columns: 1fr; }
  .brand-row { min-height: 2.6rem; }
  .header-tools { justify-self: stretch; grid-template-columns: minmax(105px, 0.42fr) minmax(0, 1fr); }
  .nav-tabs { grid-row: auto; grid-column: auto; overflow-x: auto; scrollbar-width: none; }
  .nav-tabs::-webkit-scrollbar { display: none; }
  .section-heading { align-items: flex-end; }
  .section-heading .button { flex: 0 0 auto; }
  .data-grid .button { width: 100%; }
  dialog { width: min(100% - 1rem, 680px); max-height: calc(100dvh - 1rem); border-radius: 16px; }
  dialog form { padding: 1rem; }
  .dialog-actions { position: sticky; bottom: -1rem; margin: 1rem -1rem -1rem; padding: 0.75rem 1rem max(0.75rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: blur(12px); }
  .dialog-actions .button { flex: 1; }
  .toast { left: 0.75rem; right: 0.75rem; bottom: max(0.75rem, env(safe-area-inset-bottom)); max-width: none; text-align: center; }
}

@media (max-width: 390px) {
  .header-tools { grid-template-columns: 1fr; }
  .summary-grid { gap: 0.42rem; }
  .metric strong { font-size: 1.55rem; }
  .inventory-row td { grid-template-columns: 1fr; gap: 0.22rem; }
  .inventory-row td::before { margin-bottom: 0.1rem; }
  .actions { grid-template-columns: 1fr; }
  .section-heading { align-items: stretch; flex-direction: column; }
  .section-heading .button { width: 100%; }
}

@media (prefers-reduced-motion: no-preference) {
  body, .topbar, .metric, .panel, .view, .inventory-row, input, select, textarea, .button, .icon-button, .nav-tab, .badge, .toast {
    transition: background-color 160ms ease, color 160ms ease, border-color 160ms ease, box-shadow 180ms ease, opacity 160ms ease, transform 160ms ease;
  }
  .view.is-entering { animation: view-in 190ms cubic-bezier(0.2, 0.8, 0.2, 1); }
  .metric.is-updated::after { animation: metric-update 420ms ease-out; }
  dialog[open] { animation: dialog-in 180ms cubic-bezier(0.2, 0.8, 0.2, 1); }
  dialog[open]::backdrop { animation: backdrop-in 160ms ease-out; }
  @keyframes view-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
  @keyframes metric-update { 0% { opacity: 0; transform: scaleX(0.25); } 45% { opacity: 1; transform: scaleX(1); } 100% { opacity: 0; transform: scaleX(1); } }
  @keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.985); } to { opacity: 1; transform: none; } }
  @keyframes backdrop-in { from { opacity: 0; } to { opacity: 1; } }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
