:root {
  --primary: #1e1f21;
  --primary-dark: #111213;
  --accent: #f5c518;
  --accent-dark: #d4a900;
  --bg: #f0f0f0;
  --card: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e0e0e0;
  --danger: #ef4444;
  --success: #10b981;
  --warning: #f59e0b;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.10), 0 1px 2px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.10), 0 2px 4px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}

/* ── Layout ── */
#app { display: flex; flex-direction: column; height: 100dvh; }

#topbar {
  background: var(--primary);
  color: white;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
  min-height: 56px;
}
#topbar h1 { font-size: 1.1rem; font-weight: 600; flex: 1; letter-spacing: 0.01em; }
#topbar .back-btn { background: none; border: none; color: white; font-size: 1.4rem; cursor: pointer; padding: 4px; line-height: 1; }
#topbar .topbar-action { background: none; border: none; color: white; font-size: 1.5rem; cursor: pointer; padding: 4px; line-height: 1; }

#content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: 80px;
  -webkit-overflow-scrolling: touch;
}

#bottomnav {
  position: fixed;
  bottom: 0;
  left: 0; right: 0;
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 4px;
  font-size: 0.65rem;
  color: var(--muted);
  cursor: pointer;
  border: none;
  background: none;
  gap: 2px;
  min-height: 52px;
}
.nav-item .nav-icon { font-size: 1.4rem; line-height: 1; }
.nav-item.active { color: var(--accent); }

/* ── Cards ── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.card-title { font-size: 0.75rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }

/* ── Scale Card ── */
.scale-card {
  background: linear-gradient(135deg, #2a2b2e 0%, var(--primary-dark) 100%);
  color: white;
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 12px;
  box-shadow: var(--shadow-md);
}
.scale-weight { font-size: 2.8rem; font-weight: 700; line-height: 1; color: var(--accent); }
.scale-unit { font-size: 1rem; font-weight: 400; opacity: 0.8; margin-left: 4px; }
.scale-status { font-size: 0.8rem; opacity: 0.7; margin-top: 4px; display: flex; align-items: center; gap: 6px; }
.scale-dot { width: 8px; height: 8px; border-radius: 50%; background: #6b7280; }
.scale-dot.live { background: #10b981; animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ── Stats Row ── */
.stats-row { display: flex; gap: 10px; margin-bottom: 12px; }
.stat-card { flex: 1; background: linear-gradient(135deg, #2a2b2e 0%, var(--primary-dark) 100%); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow-md); text-align: center; color: white; }
.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--accent); line-height: 1; }
.stat-label { font-size: 0.7rem; color: rgba(255,255,255,0.65); margin-top: 4px; }

/* ── Quick Actions ── */
.quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.action-btn {
  background: var(--card);
  border: none;
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: var(--shadow);
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
  transition: transform 0.1s, box-shadow 0.1s;
}
.action-btn:active { transform: scale(0.97); box-shadow: none; }
.action-btn .icon { font-size: 1.8rem; }
.action-btn.primary { background: var(--accent); color: var(--primary); }
.action-btn.accent  { background: var(--primary); color: white; }

/* ── Box List ── */
.search-bar {
  position: relative;
  margin-bottom: 12px;
}
.search-bar input {
  width: 100%;
  padding: 12px 16px 12px 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
  background: var(--card);
  color: var(--text);
  outline: none;
}
.search-bar input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(245,197,24,0.18); }
.search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); font-size: 1.1rem; color: var(--muted); pointer-events: none; }

.filter-chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: none; }
.filter-chips::-webkit-scrollbar { display: none; }
.chip {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  background: var(--card);
  color: var(--muted);
  flex-shrink: 0;
}
.chip.active { border-color: var(--accent); color: var(--primary); background: rgba(245,197,24,0.12); }

.box-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: transform 0.1s;
}
.box-item:active { transform: scale(0.98); }
.box-icon { font-size: 1.8rem; flex-shrink: 0; }
.box-info { flex: 1; min-width: 0; }
.box-label { font-weight: 600; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.box-meta { font-size: 0.8rem; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.box-right { text-align: right; flex-shrink: 0; }
.box-weight { font-weight: 600; font-size: 0.95rem; }
.box-status { display: inline-block; font-size: 0.7rem; padding: 2px 8px; border-radius: 999px; font-weight: 600; margin-top: 4px; }

/* ── Status Badges ── */
.status-full    { background: #dcfce7; color: #15803d; }
.status-partial { background: #fef9c3; color: #a16207; }
.status-empty   { background: #fee2e2; color: #b91c1c; }
.status-merged  { background: #e0e7ff; color: #4338ca; }
.status-split   { background: #fce7f3; color: #be185d; }

/* ── Box Detail ── */
.detail-hero {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.detail-label { font-size: 0.8rem; color: var(--muted); margin-bottom: 2px; }
.detail-value { font-size: 1rem; font-weight: 500; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 16px; }
.detail-weight-big { font-size: 2.2rem; font-weight: 700; color: var(--primary); }

.action-row { display: flex; gap: 8px; margin-bottom: 12px; overflow-x: auto; }
.action-row::-webkit-scrollbar { display: none; }
.btn {
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  display: flex; align-items: center; gap: 6px;
  transition: opacity 0.15s;
  flex-shrink: 0;
}
.btn:active { opacity: 0.75; }
.btn-primary { background: var(--accent); color: var(--primary); }
.btn-primary:active { background: var(--accent-dark); }
.btn-outline { background: transparent; color: var(--primary); border: 1.5px solid var(--primary); }
.btn-outline.active-view { background: var(--primary); color: white; }
.btn-danger  { background: var(--danger); color: white; }
.btn-success { background: var(--success); color: white; }
.btn-block   { width: 100%; justify-content: center; padding: 14px; font-size: 1rem; border-radius: var(--radius); }

/* ── Timeline ── */
.timeline { padding: 0; }
.timeline-item { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.timeline-item:last-child { border-bottom: none; }
.timeline-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--primary); flex-shrink: 0; margin-top: 5px; }
.timeline-body { flex: 1; }
.timeline-event { font-size: 0.85rem; font-weight: 500; }
.timeline-time  { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }

/* ── Log Table ── */
.log-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.log-table th { text-align: left; padding: 8px 10px; background: var(--primary); color: white; font-weight: 600; font-size: 0.75rem; letter-spacing: 0.03em; white-space: nowrap; }
.log-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.log-table tr:last-child td { border-bottom: none; }
.log-table tr:hover td { background: rgba(0,0,0,0.025); }
.log-table .pos { color: #22c55e; font-weight: 600; }
.log-table .neg { color: #ef4444; font-weight: 600; }

/* ── Forms ── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 0.85rem; font-weight: 600; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.04em; }
.form-control {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--card);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.form-control:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px rgba(245,197,24,0.18); }
select.form-control { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b7280' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 14px center; padding-right: 36px; }

/* ── Modal ── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: flex-end;
}
.modal-sheet {
  background: var(--card);
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom, 0));
  width: 100%;
  max-height: 90dvh;
  overflow-y: auto;
}
.modal-title { font-size: 1.1rem; font-weight: 700; margin-bottom: 20px; text-align: center; }
.modal-drag { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 0 auto 16px; }

/* ── Scanner ── */
#scanner-container { position: relative; background: black; border-radius: var(--radius); overflow: hidden; margin-bottom: 16px; }
#scanner-container video, #reader { width: 100%; max-height: 60dvh; }
.scan-overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.scan-frame { width: 200px; height: 200px; border: 2px solid var(--accent); border-radius: 12px; }
.scan-result { background: var(--card); border-radius: var(--radius); padding: 16px; text-align: center; margin-bottom: 12px; }
.scan-result-type { font-size: 0.75rem; color: var(--muted); margin-bottom: 4px; }
.scan-result-label { font-size: 1.2rem; font-weight: 700; }

/* ── Empty State ── */
.empty-state { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty-state .empty-icon { font-size: 3rem; margin-bottom: 12px; }
.empty-state p { font-size: 0.9rem; }

/* ── Loading ── */
.spinner { border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; width: 32px; height: 32px; animation: spin 0.7s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 76px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1f2937;
  color: white;
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 500;
  opacity: 0;
  transition: all 0.3s;
  z-index: 300;
  white-space: nowrap;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Type Grid (Flake Color Cards) ── */
.type-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.color-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.1s;
}
.color-card:active { transform: scale(0.97); }
.color-name { font-weight: 700; font-size: 0.95rem; margin-bottom: 4px; }
.color-aliases { font-size: 0.72rem; color: var(--muted); margin-bottom: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.color-stats { display: flex; justify-content: space-between; font-size: 0.78rem; color: var(--muted); border-top: 1px solid var(--border); padding-top: 8px; margin-top: 4px; }

/* ── Inventory Table ── */
.inv-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 4px; }
.inv-table th { text-align: left; padding: 6px 8px; font-size: 0.7rem; color: var(--muted); text-transform: uppercase; border-bottom: 1px solid var(--border); font-weight: 600; }
.inv-table td { padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.inv-table tr:last-child td { border-bottom: none; }
.inv-table tr:nth-child(even) td { background: var(--bg); }
.loc-select { font-size: 0.82rem; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--card); color: var(--text); cursor: pointer; max-width: 140px; width: 100%; }
.bulk-result-row { padding: 10px 12px; cursor: pointer; border-radius: 8px; font-size: 0.9rem; display: flex; align-items: center; gap: 8px; }
.bulk-result-row:hover { background: var(--bg); }
.bulk-result-type { font-size: 0.7rem; padding: 2px 6px; border-radius: 10px; background: var(--primary); color: #fff; flex-shrink: 0; text-transform: capitalize; }
.bulk-row-sub { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }

/* ── Form Row (side-by-side fields) ── */
.form-row { display: flex; gap: 10px; }
.form-row .form-group { flex: 1; min-width: 0; }

/* ── Inline link button ── */
.btn-link { background: none; border: none; color: var(--primary); font-size: 0.8rem; cursor: pointer; padding: 0; text-decoration: underline; }

/* ── Stat sub-label ── */
.stat-sub { font-size: 0.65rem; color: rgba(255,255,255,0.5); margin-top: 2px; }

/* ── Log Page ── */
.log-item { padding: 12px 0 10px; }
.log-sep  { height: 1px; background: var(--border); }

/* ── Section labels ── */
.section-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
  padding: 0 2px;
}

/* ── Responsive / Desktop ── */
@media (min-width: 600px) {
  #content { padding: 20px 24px 100px; }

  .type-grid {
    grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
    gap: 12px;
  }

  .quick-actions { grid-template-columns: repeat(4, 1fr); }

  .card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 12px;
  }
  .card-grid > .card { margin-bottom: 0; }

  .modal-overlay { align-items: center; }
  .modal-sheet {
    border-radius: 20px;
    max-width: 540px;
    margin: 0 auto;
    max-height: 85dvh;
  }
}

@media (min-width: 900px) {
  #content { padding: 24px 32px 100px; }
  .type-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
  .card-grid  { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
}

@media (min-width: 1200px) {
  #content { padding: 28px 40px 100px; }
  .type-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
  .card-grid  { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }
}

/* ── Login Screen ──────────────────────────────────────────────────────────── */

#login-overlay {
  position: fixed;
  inset: 0;
  background: var(--primary-dark);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}

.login-box {
  background: var(--card);
  border-radius: 20px;
  padding: 36px 28px 28px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}

.login-logo {
  text-align: center;
  margin-bottom: 32px;
}

.login-logo-mark {
  width: 56px;
  height: 56px;
  background: var(--primary);
  border-radius: 14px;
  margin: 0 auto 14px;
  position: relative;
}

.login-logo-mark::after {
  content: '';
  position: absolute;
  inset: 10px;
  border-radius: 4px;
  background: var(--accent);
}

.login-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.login-subtitle {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

.login-error {
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.85rem;
  margin-bottom: 14px;
  text-align: center;
}

#login-form .form-group { margin-bottom: 14px; }
