/* ============================================================
   Scanner — Mobile-First Inventory Scanner UI
   Cream/Sage theme matching BloomBooks
   ============================================================ */

:root, [data-theme="light"] {
  --bg-primary: #fafaf9; --bg-secondary: #ffffff; --bg-tertiary: #f5f3f0;
  --bg-card: #ffffff; --bg-hover: #f0eeeb; --bg-input: #ffffff;
  --border: #e8e4df; --border-light: #f0eeeb;
  --text-primary: #2d2d2d; --text-secondary: #6b6b6b; --text-muted: #a0a0a0; --text-link: #5a7d60;
  --accent: #6b8f71; --accent-end: #5a7d60;
  --accent-gradient: linear-gradient(135deg, #6b8f71, #5a7d60);
  --accent-soft: rgba(107,143,113,0.12);
  --success: #6b8f71; --warning: #d4a574; --danger: #c47070; --info: #7ca8c6;
  --sidebar-w: 240px; --topbar-h: 56px; --bottom-nav-h: 64px;
  --radius: 12px; --radius-sm: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.06); --shadow-lg: 0 8px 24px rgba(0,0,0,0.08);
  --transition: 0.2s ease;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

[data-theme="dark"] {
  --bg-primary: #1a1a1a; --bg-secondary: #242424; --bg-tertiary: #2e2e2e;
  --bg-card: #242424; --bg-hover: #333333; --bg-input: #1a1a1a;
  --border: #3a3a3a; --border-light: #2e2e2e;
  --text-primary: #e8e4df; --text-secondary: #a0a0a0; --text-muted: #6b6b6b; --text-link: #8fb896;
  --accent: #8fb896; --accent-end: #7da886;
  --accent-gradient: linear-gradient(135deg, #8fb896, #7da886);
  --accent-soft: rgba(143,184,150,0.15);
  --success: #8fb896; --warning: #d4a574; --danger: #d48080; --info: #8fb8d4;
  --shadow: 0 2px 8px rgba(0,0,0,0.3); --shadow-lg: 0 8px 24px rgba(0,0,0,0.4);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }
body { font-family: var(--font); background: var(--bg-primary); color: var(--text-primary); line-height: 1.6; overflow: hidden; height: 100vh; }
a { color: var(--text-link); text-decoration: none; }
button { cursor: pointer; font-family: inherit; touch-action: manipulation; }
input, select, textarea {
  font-family: inherit; font-size: 16px; background: var(--bg-input); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 16px; outline: none;
  transition: border var(--transition);
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(107,143,113,0.12); }
.hidden { display: none !important; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 20px; border: none; border-radius: var(--radius-sm);
  font-size: 1rem; font-weight: 600; transition: all var(--transition);
  white-space: nowrap; min-height: 48px; touch-action: manipulation;
}
.btn-primary { background: var(--accent-gradient); color: #fff; }
.btn-primary:hover, .btn-primary:active { filter: brightness(1.05); transform: translateY(-1px); }
.btn-secondary { background: var(--bg-tertiary); color: var(--text-primary); border: 1px solid var(--border); }
.btn-secondary:hover, .btn-secondary:active { background: var(--bg-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-lg { padding: 14px 24px; font-size: 1.1rem; min-height: 56px; }
.btn-sm { padding: 8px 14px; font-size: 0.85rem; min-height: 40px; }
.btn-full { width: 100%; }

/* Login */
.login-screen {
  display: flex; align-items: center; justify-content: center; height: 100vh; background: #fafaf9;
  background-image: radial-gradient(circle at 30% 50%, rgba(107,143,113,0.07) 0%, transparent 60%);
}
[data-theme="dark"] .login-screen { background: #1a1a1a; background-image: radial-gradient(circle at 30% 50%, rgba(143,184,150,0.06) 0%, transparent 60%); }
.login-card { background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius); padding: 44px; width: 400px; max-width: 95vw; box-shadow: var(--shadow-lg); text-align: center; }
.login-logo { font-size: 3.5rem; margin-bottom: 8px; }
.login-title { font-size: 1.5rem; font-weight: 700; background: var(--accent-gradient); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.login-subtitle { color: var(--text-secondary); margin-bottom: 28px; font-size: 0.95rem; }
.login-tabs { display: flex; gap: 4px; margin-bottom: 20px; background: var(--bg-tertiary); border-radius: var(--radius-sm); padding: 3px; }
.login-tab { flex: 1; padding: 10px; border: none; background: transparent; color: var(--text-secondary); border-radius: var(--radius-sm); font-size: 0.9rem; min-height: 44px; }
.login-tab.active { background: var(--bg-secondary); color: var(--text-primary); box-shadow: 0 1px 3px rgba(0,0,0,0.06); }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form input { width: 100%; padding: 14px 16px; }
.login-error { color: var(--danger); font-size: 0.85rem; padding: 8px 12px; background: rgba(196,112,112,0.08); border-radius: var(--radius-sm); }

/* App Shell */
.app-shell { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar — hidden by default on mobile */
.sidebar {
  width: var(--sidebar-w); background: var(--bg-secondary); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; transition: left 0.25s ease; overflow: hidden; flex-shrink: 0; z-index: 200;
  position: fixed; left: -260px; top: 0; bottom: 0;
}
.sidebar.mobile-open { left: 0; box-shadow: 4px 0 20px rgba(0,0,0,0.15); }
.sidebar-header { display: flex; align-items: center; gap: 10px; padding: 16px; border-bottom: 1px solid var(--border); min-height: var(--topbar-h); }
.sidebar-logo { font-size: 1.5rem; }
.sidebar-brand { font-size: 1.1rem; font-weight: 700; background: var(--accent-gradient); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.sidebar-collapse-btn { margin-left: auto; background: none; border: none; color: var(--text-secondary); font-size: 1.2rem; min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
.sidebar-nav { flex: 1; padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.nav-item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: var(--radius-sm); color: var(--text-secondary); transition: all var(--transition); text-decoration: none; border: none; background: none; font-size: 1rem; width: 100%; text-align: left; min-height: 48px; }
.nav-item:hover { background: var(--bg-hover); color: var(--text-primary); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-icon { font-size: 1.3rem; flex-shrink: 0; width: 28px; text-align: center; }
.nav-label { white-space: nowrap; }
.sidebar-footer { padding: 8px; border-top: 1px solid var(--border); }
.logout-btn { color: var(--text-muted) !important; }
.sidebar-divider { height: 1px; background: var(--border); margin: 12px 16px; }
.sidebar-apps .nav-item { font-size: 0.9rem; padding: 10px 14px; opacity: 0.75; }
.sidebar-apps .nav-item:hover { opacity: 1; }
.sidebar-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.3); z-index: 199; }
.sidebar.mobile-open ~ .sidebar-overlay { display: block; }

/* Main Wrapper */
.main-wrapper { flex: 1; display: flex; flex-direction: column; overflow: hidden; width: 100%; }

/* Topbar */
.topbar { height: var(--topbar-h); display: flex; align-items: center; padding: 0 16px; border-bottom: 1px solid var(--border); background: var(--bg-secondary); flex-shrink: 0; gap: 12px; }
.mobile-menu-btn { display: block; background: none; border: none; color: var(--text-primary); font-size: 1.4rem; min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
.page-title { font-size: 1.15rem; font-weight: 700; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.topbar-apps { display: flex; gap: 4px; }
.topbar-app-link { padding: 4px 8px; border-radius: 6px; font-size: 0.78rem; color: var(--text-secondary); background: var(--bg-tertiary); border: 1px solid var(--border); white-space: nowrap; text-decoration: none; min-height: 32px; display: flex; align-items: center; }
.topbar-icon { background: none; border: none; font-size: 1.2rem; color: var(--text-secondary); min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
.user-pill { display: flex; align-items: center; gap: 6px; padding: 4px 10px 4px 4px; background: var(--bg-tertiary); border-radius: 20px; }
.user-avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent-gradient); display: flex; align-items: center; justify-content: center; font-size: 0.8rem; font-weight: 600; color: #fff; flex-shrink: 0; }
.user-name { font-size: 0.85rem; color: var(--text-secondary); }

/* Content */
.content { flex: 1; overflow-y: auto; padding: 16px; padding-bottom: calc(var(--bottom-nav-h) + 24px); -webkit-overflow-scrolling: touch; }

/* Bottom Nav */
.bottom-nav {
  display: flex; position: fixed; bottom: 0; left: 0; right: 0; height: var(--bottom-nav-h);
  background: var(--bg-secondary); border-top: 1px solid var(--border); z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottom-nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: none; border: none; color: var(--text-muted); font-size: 0.7rem; padding: 8px 4px;
  transition: color var(--transition); min-height: 48px;
}
.bottom-nav-item.active { color: var(--accent); }
.bottom-nav-icon { font-size: 1.4rem; }
.bottom-nav-label { font-weight: 600; }

/* Cards */
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.card-title { font-size: 1.05rem; font-weight: 700; }

/* Toast */
.toast-container { position: fixed; bottom: calc(var(--bottom-nav-h) + 12px); left: 50%; transform: translateX(-50%); z-index: 2000; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { padding: 12px 24px; border-radius: 24px; font-size: 0.95rem; color: #fff; animation: slideUp 0.2s ease; box-shadow: var(--shadow-lg); max-width: 90vw; text-align: center; }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-info { background: var(--info); }
.toast-warning { background: var(--warning); }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: flex-end; justify-content: center; z-index: 1000; backdrop-filter: blur(2px); }
.modal { background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 20px 20px 0 0; width: 100%; max-width: 560px; max-height: 90vh; display: flex; flex-direction: column; box-shadow: var(--shadow-lg); animation: slideUp 0.25s ease; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 1.1rem; font-weight: 700; }
.modal-close { background: none; border: none; color: var(--text-secondary); font-size: 1.3rem; min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
.modal-body { padding: 20px; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.9rem; color: var(--text-secondary); margin-bottom: 6px; font-weight: 600; }
.form-group input, .form-group select { width: 100%; }

/* Toolbar */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.search-input { flex: 1; min-width: 180px; padding: 12px 16px 12px 40px; font-size: 1rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23a0a0a0'%3E%3Ccircle cx='7' cy='7' r='5.5' stroke='%23a0a0a0' stroke-width='1.5' fill='none'/%3E%3Cline x1='11' y1='11' x2='15' y2='15' stroke='%23a0a0a0' stroke-width='1.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: 14px center;
}
.count-filter { padding: 12px 14px; min-width: 120px; }

/* Spinner */
.spinner { display: inline-block; width: 24px; height: 24px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-center { display: flex; justify-content: center; padding: 40px; }

/* ============================================================
   SCAN VIEW
   ============================================================ */
.scan-page { max-width: 600px; margin: 0 auto; }

.scan-viewfinder-wrap { margin-bottom: 16px; }

.scan-reader { width: 100%; min-height: 200px; border-radius: var(--radius); overflow: hidden; background: #000; margin-bottom: 12px; }
.scan-reader video { width: 100% !important; border-radius: var(--radius); }

.scan-manual { display: flex; gap: 8px; margin-bottom: 12px; }
.scan-input { flex: 1; padding: 14px 16px; font-size: 1.1rem; border-radius: var(--radius-sm); }

.scan-camera-controls { display: flex; gap: 8px; }

/* Scan Result */
.scan-result { margin-bottom: 20px; }
.result-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.result-found { border-left: 4px solid var(--success); }
.result-not-found { border-left: 4px solid var(--warning); }
.result-error { border-left: 4px solid var(--danger); }

.result-header { display: flex; gap: 16px; align-items: center; margin-bottom: 16px; }
.result-img { width: 64px; height: 64px; border-radius: var(--radius-sm); background: var(--bg-tertiary); display: flex; align-items: center; justify-content: center; font-size: 2rem; flex-shrink: 0; overflow: hidden; }
.result-img img { width: 100%; height: 100%; object-fit: cover; }
.result-info { flex: 1; min-width: 0; }
.result-name { font-size: 1.2rem; font-weight: 700; }
.result-meta { font-size: 0.9rem; color: var(--text-secondary); }
.result-barcode { font-size: 0.8rem; color: var(--text-muted); font-family: monospace; }

.result-qty-display { text-align: center; padding: 16px; background: var(--bg-tertiary); border-radius: var(--radius-sm); margin-bottom: 16px; }
.result-qty-label { display: block; font-size: 0.85rem; color: var(--text-secondary); margin-bottom: 4px; }
.result-qty-value { font-size: 3rem; font-weight: 800; background: var(--accent-gradient); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }

.result-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.result-actions .btn { font-size: 1.1rem; }

/* Scan History */
.scan-section-title { font-size: 1rem; font-weight: 700; color: var(--text-secondary); margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.03em; }
.scan-history-list { display: flex; flex-direction: column; gap: 4px; }
.history-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-sm); cursor: pointer; min-height: 52px; }
.history-row:active { background: var(--bg-hover); }
.history-info { flex: 1; min-width: 0; }
.history-name { font-weight: 600; font-size: 0.95rem; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-barcode { font-size: 0.8rem; color: var(--text-muted); font-family: monospace; }
.history-right { text-align: right; flex-shrink: 0; margin-left: 12px; }
.history-qty { font-weight: 700; font-size: 0.9rem; color: var(--accent); display: block; }
.history-qty.not-found { color: var(--warning); }
.history-time { font-size: 0.75rem; color: var(--text-muted); }
.empty-state-sm { text-align: center; padding: 24px; color: var(--text-muted); font-size: 0.95rem; }

/* ============================================================
   COUNT & LOOKUP VIEWS
   ============================================================ */
.count-list { display: flex; flex-direction: column; gap: 6px; }
.count-item { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-sm); cursor: pointer; min-height: 60px; gap: 12px; }
.count-item:active { background: var(--bg-hover); }
.count-item-left { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; }
.count-item-img { width: 48px; height: 48px; border-radius: var(--radius-sm); background: var(--bg-tertiary); display: flex; align-items: center; justify-content: center; font-size: 1.4rem; flex-shrink: 0; overflow: hidden; }
.count-item-img img { width: 100%; height: 100%; object-fit: cover; }
.count-item-info { flex: 1; min-width: 0; }
.count-item-name { font-weight: 600; font-size: 1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count-item-meta { font-size: 0.8rem; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count-item-counted { font-size: 0.75rem; color: var(--success); }
.count-item-counted .not-counted { color: var(--text-muted); }
.count-item-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.count-item-qty { font-size: 0.9rem; color: var(--text-secondary); white-space: nowrap; }
.count-item-qty strong { font-size: 1.1rem; color: var(--text-primary); }
.count-item-qty.low-stock strong { color: var(--danger); }
.count-btn { min-width: 70px; }
.count-current { font-size: 1.1rem; text-align: center; padding: 12px; background: var(--bg-tertiary); border-radius: var(--radius-sm); }

/* ============================================================
   HISTORY VIEW
   ============================================================ */
.history-log-row { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-sm); margin-bottom: 6px; gap: 12px; min-height: 56px; }
.hist-left { flex: 1; min-width: 0; }
.hist-action-badge { display: inline-block; padding: 2px 10px; border-radius: 10px; font-size: 0.75rem; font-weight: 700; text-transform: capitalize; background: var(--accent-soft); color: var(--accent); margin-bottom: 4px; }
.hist-action-badge.count { background: rgba(124,168,198,0.12); color: var(--info); }
.hist-action-badge.scan_adjust { background: rgba(107,143,113,0.12); color: var(--success); }
.hist-action-badge.manual_adjust { background: rgba(212,165,116,0.12); color: var(--warning); }
.hist-item-name { font-weight: 600; font-size: 0.95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-notes { font-size: 0.8rem; color: var(--text-muted); }
.hist-right { text-align: right; flex-shrink: 0; }
.hist-change { font-weight: 700; font-size: 1rem; }
.hist-change.positive { color: var(--success); }
.hist-change.negative { color: var(--danger); }
.hist-after { font-size: 0.8rem; color: var(--text-secondary); }
.hist-time { font-size: 0.75rem; color: var(--text-muted); }
.hist-user { font-size: 0.7rem; color: var(--text-muted); }

/* ============================================================
   SETTINGS VIEW
   ============================================================ */
.settings-page { max-width: 560px; margin: 0 auto; }
.setting-row { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid var(--border-light); min-height: 56px; }
.setting-row:last-child { border-bottom: none; }
.setting-info { flex: 1; }
.setting-label { font-weight: 600; font-size: 1rem; }
.setting-desc { font-size: 0.8rem; color: var(--text-secondary); }
.setting-select { padding: 8px 12px; min-width: 140px; }
.toggle { position: relative; width: 50px; height: 28px; flex-shrink: 0; display: inline-block; }
.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle-slider { position: absolute; cursor: pointer; inset: 0; background: #ccc; border-radius: 28px; transition: 0.2s; }
.toggle-slider:before { content: ''; position: absolute; height: 22px; width: 22px; left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: 0.2s; }
.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider:before { transform: translateX(22px); }

/* Detail Grid */
.detail-grid { display: flex; flex-direction: column; gap: 8px; }
.detail-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border-light); font-size: 0.95rem; }
.detail-row:last-child { border-bottom: none; }
.detail-label { color: var(--text-secondary); font-weight: 500; }
.detail-qty { font-size: 1.3rem; font-weight: 800; color: var(--accent); }
.variant-list { display: flex; flex-direction: column; gap: 4px; }
.variant-row { display: flex; justify-content: space-between; padding: 8px 12px; background: var(--bg-tertiary); border-radius: var(--radius-sm); font-size: 0.9rem; }

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

/* Utilities */
.mt-16 { margin-top: 16px; }
.mb-24 { margin-bottom: 24px; }

/* Animations */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }

/* Desktop overrides */
@media (min-width: 769px) {
  .sidebar { position: relative; left: 0; }
  .sidebar-overlay { display: none !important; }
  .bottom-nav { display: none; }
  .content { padding: 24px; padding-bottom: 24px; }
  .topbar-apps { display: flex; }
  .modal-overlay { align-items: center; }
  .modal { border-radius: var(--radius); max-width: 560px; }
  .toast-container { bottom: 20px; }
}

@media (max-width: 768px) {
  .topbar-apps { display: none; }
  .user-name { display: none; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar .search-input { min-width: unset; }
}

@media (max-width: 400px) {
  .result-actions { grid-template-columns: 1fr 1fr; gap: 8px; }
  .result-actions .btn { font-size: 0.95rem; padding: 12px 8px; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* SSO Bridge script may not exist — that's okay */
