
:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --danger: #dc2626;
  --warning: #f59e0b;
  --warning-bg: #fff7ed;
  --success: #059669;
  --shadow: 0 12px 30px rgba(17, 24, 39, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; }

button { cursor: pointer; }

.app-shell {
  width: min(1100px, 100%);
  margin: 0 auto;
  padding: 20px;
  padding-bottom: calc(32px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

h1, h2, p { margin-top: 0; }

h1 {
  font-size: clamp(1.8rem, 5vw, 2.6rem);
  margin-bottom: 0;
}

h2 { margin-bottom: 6px; }

.eyebrow {
  margin-bottom: 4px;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 0.84rem;
  margin-bottom: 6px;
}

.stat-card strong { font-size: 1.65rem; }

.stat-card.warning strong { color: var(--warning); }

.toolbar {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.search-wrap {
  position: relative;
}

.search-wrap input {
  width: 100%;
  padding: 13px 40px 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
}

.clear-search-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  padding: 6px 10px;
  border-radius: 8px;
}

.clear-search-btn:hover { background: var(--surface); }

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 10px;
  margin-top: 10px;
}

select, input, textarea {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  background: white;
  color: var(--text);
}

textarea { resize: vertical; }

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
  background: #e5e7eb;
  padding: 4px;
  border-radius: 12px;
  width: fit-content;
}

.tab {
  border: 0;
  background: transparent;
  border-radius: 9px;
  padding: 9px 16px;
  font-weight: 700;
  color: var(--muted);
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(17, 24, 39, .08);
}

.view { display: none; }
.view.active { display: block; }

.card-list {
  display: grid;
  gap: 12px;
}

.stock-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.stock-card.status-red {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
  background: color-mix(in srgb, var(--danger) 7%, var(--surface));
}

.stock-card.status-amber {
  border-color: color-mix(in srgb, var(--warning) 45%, var(--line));
  background: color-mix(in srgb, var(--warning) 7%, var(--surface));
}

.card-top-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.tap-hint {
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
}

#receiveForm { padding: 18px; }

.card-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.card-title {
  margin: 0 0 5px;
  font-size: 1.08rem;
}

.part-number-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 6px;
}

.part-number-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
  text-transform: uppercase;
}

.part-number-value {
  font-family: "SFMono-Regular", ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--line);
}

.part-number-empty {
  font-style: italic;
  font-weight: 400;
  color: var(--muted);
}

.meta {
  color: var(--muted);
  font-size: 0.83rem;
  line-height: 1.45;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 5px 9px;
  font-size: 0.74rem;
  font-weight: 800;
  white-space: nowrap;
  background: color-mix(in srgb, var(--success) 18%, white);
  color: color-mix(in srgb, var(--success) 55%, black);
}

.badge.status-amber {
  background: color-mix(in srgb, var(--warning) 22%, white);
  color: color-mix(in srgb, var(--warning) 45%, black);
}

.badge.status-red {
  background: color-mix(in srgb, var(--danger) 18%, white);
  color: color-mix(in srgb, var(--danger) 42%, black);
}

.level-bar {
  margin-top: 12px;
  height: 8px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.level-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width .2s ease, background-color .2s ease;
  background: var(--success);
}

.level-bar-fill.status-red { background: var(--danger); }
.level-bar-fill.status-amber { background: var(--warning); }

.quantity-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.quantity {
  font-size: 1.5rem;
  font-weight: 800;
}

.quantity small {
  font-size: .8rem;
  color: var(--muted);
  font-weight: 600;
}

.qty-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.primary-btn, .secondary-btn, .ghost-btn, .danger-btn, .qty-btn, .icon-btn {
  border-radius: 10px;
  padding: 10px 13px;
  font-weight: 750;
}

.primary-btn {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: white;
}

.primary-btn:hover { background: var(--accent-dark); }

.secondary-btn {
  border: 1px solid var(--line);
  background: white;
  color: var(--text);
}

.ghost-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
}

.danger-btn {
  border: 1px solid #fecaca;
  background: #fef2f2;
  color: var(--danger);
}

.qty-btn {
  border: 1px solid var(--line);
  background: white;
  min-width: 42px;
}

.qty-btn.use {
  background: #fff;
  color: var(--danger);
}

.qty-btn.add { color: var(--success); }

.icon-btn {
  border: 0;
  background: transparent;
  font-size: 1.8rem;
  line-height: 1;
}

.section-heading {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 12px;
  margin-bottom: 12px;
}

.section-heading p { color: var(--muted); }

.restock-line {
  margin-top: 10px;
  font-weight: 800;
  color: #9a3412;
}

.data-tools {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.file-label input { display: none; }

.empty-state {
  text-align: center;
  padding: 44px 20px;
  color: var(--muted);
}

.empty-state .secondary-btn {
  margin-top: 6px;
}

.first-run-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 16px;
  flex-wrap: wrap;
}

.hidden { display: none !important; }

.confirm-dialog {
  width: min(420px, calc(100% - 24px));
  border: 0;
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 28px 80px rgba(0,0,0,.25);
}

.confirm-dialog::backdrop {
  background: rgba(17, 24, 39, .55);
  backdrop-filter: blur(3px);
}

.confirm-body p {
  color: var(--muted);
}

dialog {
  width: min(720px, calc(100% - 24px));
  border: 0;
  border-radius: 18px;
  padding: 0;
  box-shadow: 0 28px 80px rgba(0,0,0,.25);
}

dialog::backdrop {
  background: rgba(17, 24, 39, .55);
  backdrop-filter: blur(3px);
}

#partForm { padding: 18px; }

.dialog-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: .84rem;
  font-weight: 700;
}

.form-grid .full { grid-column: 1 / -1; }

.dialog-actions {
  margin-top: 18px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.dialog-actions-right {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #111827;
  color: white;
  padding: 11px 15px;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: .2s ease;
  z-index: 999;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: calc(100vw - 32px);
}

.toast.show { pointer-events: auto; }

.toast-undo-btn {
  border: 0;
  background: transparent;
  color: #93c5fd;
  font-weight: 800;
  padding: 2px 4px;
  flex-shrink: 0;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.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;
}

@media (max-width: 700px) {
  .app-shell { padding: 14px; }
  .topbar { align-items: flex-end; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .stat-card:last-child { grid-column: 1 / -1; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters select { grid-column: span 1; }
  .toggle { grid-column: 1 / -1; padding: 6px 2px 0; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: auto; }
  .quantity-row { align-items: flex-end; }
}

@media (max-width: 460px) {
  .topbar { align-items: stretch; flex-direction: column; }
  .topbar .primary-btn { width: 100%; }
  .quantity-row { flex-direction: column; align-items: stretch; }
  .qty-actions { justify-content: stretch; }
  .qty-actions button { flex: 1; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions-right { margin-left: 0; }
  .dialog-actions-right button, .danger-btn { flex: 1; }
}

/* Settings page */

.settings-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  margin-bottom: 16px;
}

.settings-card h3 {
  margin: 0 0 4px;
}

.settings-hint {
  color: var(--muted);
  font-size: 0.88rem;
  margin: 0 0 14px;
}

.preset-row {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.preset-swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid var(--line);
  padding: 0;
}

.preset-swatch:hover, .preset-swatch:focus-visible {
  border-color: var(--accent);
}

.color-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}

.color-row:first-of-type { border-top: none; }

.color-row input[type="color"] {
  width: 44px;
  height: 34px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

/* History & Usage */

.history-filters {
  grid-template-columns: 1.4fr 1fr 1fr;
}

.date-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.78rem;
  color: var(--muted);
}

.date-field input {
  padding: 10px 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.9rem;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-row {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.history-row-main {
  flex: 1;
  min-width: 0;
}

.history-row-top {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}

.history-type-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 0.68rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .03em;
  white-space: nowrap;
}

.history-type-badge.added,
.history-type-badge.opening {
  background: color-mix(in srgb, var(--success) 18%, white);
  color: color-mix(in srgb, var(--success) 55%, black);
}

.history-type-badge.used {
  background: color-mix(in srgb, var(--danger) 15%, white);
  color: color-mix(in srgb, var(--danger) 42%, black);
}

.history-type-badge.adjustment {
  background: color-mix(in srgb, var(--warning) 20%, white);
  color: color-mix(in srgb, var(--warning) 45%, black);
}

.history-part-name {
  font-weight: 700;
}

.history-meta {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.history-note {
  font-size: 0.82rem;
  font-style: italic;
  color: var(--muted);
  margin-top: 2px;
}

.history-qty {
  text-align: right;
  white-space: nowrap;
  flex-shrink: 0;
}

.history-qty-change {
  font-weight: 800;
  font-size: 1rem;
}

.history-qty-change.positive { color: var(--success); }
.history-qty-change.negative { color: var(--danger); }

.history-qty-trail {
  font-size: 0.76rem;
  color: var(--muted);
  margin-top: 2px;
}

.history-delete-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 4px 6px;
  border-radius: 8px;
  margin-top: 4px;
}

.history-delete-btn:hover {
  background: color-mix(in srgb, var(--danger) 12%, white);
  color: var(--danger);
}

.usage-card { margin-top: 16px; }
.usage-card .section-heading { margin-bottom: 12px; }
.usage-card select { max-width: 170px; }

.usage-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.usage-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}

.usage-row:first-child { border-top: none; }

.usage-rank {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.76rem;
  font-weight: 800;
  color: var(--muted);
  flex-shrink: 0;
}

.usage-row-main {
  flex: 1;
  min-width: 0;
}

.usage-row-stats {
  text-align: right;
  font-size: 0.8rem;
  color: var(--muted);
  flex-shrink: 0;
}

.usage-row-stats strong {
  display: block;
  font-size: 0.95rem;
  color: var(--text);
}

@media (max-width: 640px) {
  .history-filters { grid-template-columns: 1fr 1fr; }
  .history-row { flex-direction: column; }
  .history-qty { text-align: left; }
}

/* Auth screen */

.auth-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 20px;
  padding-top: calc(20px + env(safe-area-inset-top));
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 1000;
}

.auth-screen.hidden { display: none; }

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 26px 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-card h1 {
  margin: 0 0 2px;
}

.auth-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.auth-card input {
  padding: 12px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 1rem;
  color: var(--text);
}

.auth-submit-btn {
  width: 100%;
  margin-top: 4px;
}

.auth-error {
  background: color-mix(in srgb, var(--danger) 12%, white);
  color: color-mix(in srgb, var(--danger) 45%, black);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.85rem;
}

/* Migration banner */

.migration-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  background: color-mix(in srgb, var(--warning) 12%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--warning) 45%, var(--line));
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 16px;
}

.migration-banner.hidden { display: none; }

.migration-banner-text p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.migration-banner-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

@media (max-width: 520px) {
  .migration-banner { flex-direction: column; align-items: stretch; }
  .migration-banner-actions { justify-content: flex-end; }
}

#migrationError { margin-bottom: 16px; }

/* Sync status indicator */

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sync-indicator {
  font-size: 0.76rem;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

.sync-indicator.hidden { display: none; }

.sync-indicator.saving {
  background: var(--surface-2);
  color: var(--muted);
}

.sync-indicator.saved {
  background: color-mix(in srgb, var(--success) 18%, white);
  color: color-mix(in srgb, var(--success) 55%, black);
}

.sync-indicator.error {
  background: color-mix(in srgb, var(--danger) 15%, white);
  color: color-mix(in srgb, var(--danger) 42%, black);
}

/* Connection status */

.connection-status {
  font-size: 0.76rem;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.connection-status.hidden { display: none; }

.connection-status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.connection-status.online {
  background: color-mix(in srgb, var(--success) 15%, white);
  color: color-mix(in srgb, var(--success) 55%, black);
}

.connection-status.offline {
  background: color-mix(in srgb, var(--danger) 14%, white);
  color: color-mix(in srgb, var(--danger) 45%, black);
}

.connection-status.pending {
  background: color-mix(in srgb, var(--warning) 20%, white);
  color: color-mix(in srgb, var(--warning) 45%, black);
}

.pending-sync-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.7rem;
  font-weight: 700;
  color: color-mix(in srgb, var(--warning) 45%, black);
  background: color-mix(in srgb, var(--warning) 18%, white);
  border-radius: 999px;
  padding: 3px 8px;
  margin-top: 6px;
}
