:root {
    --primary: #4A6CF7;
    --primary-dark: #3A56D4;
    --primary-light: #E8EDFD;
    --success: #34C759;
    --danger: #FF3B30;
    --warning: #FF9500;
    --bg: #F2F4F8;
    --card-bg: #FFFFFF;
    --text-primary: #1A1A2E;
    --text-secondary: #6B7280;
    --text-muted: #9CA3AF;
    --border: #E5E7EB;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    --radius: 16px;
    --radius-xs: 6px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
    --app-height: 100vh;
}

[data-theme="dark"] {
    --primary: #6B8AFF;
    --primary-dark: #5A78E0;
    --primary-light: #2A3A6A;
    --success: #30D158;
    --danger: #FF453A;
    --warning: #FF9F0A;
    --bg: #121212;
    --card-bg: #1E1E1E;
    --text-primary: #F5F5F7;
    --text-secondary: #A1A1A6;
    --text-muted: #6E6E73;
    --border: #2C2C2E;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

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

html, body { height: 100%; overflow: hidden; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
    background: var(--bg);
    color: var(--text-primary);
    height: var(--app-height);
    display: flex;
    flex-direction: column;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    user-select: none;
    -webkit-user-select: none;
    overscroll-behavior: none;
    transition: background 0.3s, color 0.3s;
}

/* Header */
.header {
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    z-index: 10;
    flex-shrink: 0;
}
.header-icon {
    width: 36px; height: 36px; border-radius: 10px;
    background: var(--primary-light);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.header-title { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; line-height: 1.2; }
.header-subtitle { font-size: 11px; color: var(--text-secondary); }
.header-count, .header-badge {
    margin-left: auto;
    background: var(--primary-light);
    color: var(--primary);
    font-size: 13px; font-weight: 700;
    padding: 5px 12px; border-radius: 20px;
    white-space: nowrap;
}
.theme-toggle {
    background: none; border: none; cursor: pointer;
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    color: var(--text-secondary);
    transition: all 0.2s;
}
.theme-toggle:hover { background: var(--primary-light); color: var(--primary); }

/* Main */
.main {
    flex: 1; display: flex; flex-direction: column;
    padding: 12px 14px; gap: 12px;
    max-width: 800px; width: 100%;
    margin: 0 auto; min-height: 0;
}

/* Records / Lists */
.records-section, .list-section { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.records-header, .section-header {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 8px; padding: 0 2px; flex-shrink: 0;
}
.records-title, .section-title { font-size: 15px; font-weight: 700; }
.records-actions, .section-actions { display: flex; gap: 6px; }
.action-btn, a.action-btn {
    border: none;
    background: var(--card-bg);
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
    padding: 7px 12px;
    border-radius: var(--radius-xs);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: all 0.2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    border: 1px solid var(--border);
    text-decoration: none;
    font-family: inherit;
    line-height: 1;
}
.action-btn:active { transform: scale(0.95); }
.action-btn.primary-btn { color: var(--primary); border-color: var(--primary-light); background: var(--primary-light); }
.action-btn.danger-btn { color: var(--danger); border-color: #FDE8E8; background: #FEF5F5; }
.action-btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.records-list-container, .list-container {
    flex: 1; overflow-y: auto;
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    -webkit-overflow-scrolling: touch;
    min-height: 0;
}
.records-list, .item-list { list-style: none; padding: 4px 0; }
.record-item, .list-item {
    display: flex; align-items: flex-start; gap: 10px;
    padding: 11px 14px;
    border-bottom: 1px solid var(--border);
    animation: slide-in 0.3s ease-out;
    cursor: pointer;
}
.record-item:last-child, .list-item:last-child { border-bottom: none; }
.list-item:hover { background: var(--primary-light); }
@keyframes slide-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}
.record-index, .item-index {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--primary-light); color: var(--primary);
    font-size: 12px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; margin-top: 2px;
}
.record-body, .item-body { flex: 1; min-width: 0; }
.record-content, .item-title { font-size: 14px; font-weight: 600; word-break: break-all; line-height: 1.45; }
.record-meta, .item-meta { font-size: 11px; color: var(--text-muted); margin-top: 3px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.record-badge, .badge {
    display: inline-block; font-size: 10px; font-weight: 700;
    padding: 2px 7px; border-radius: 20px;
}
.badge-duplicate { background: #FEF3E2; color: #E67E22; border: 1px solid #FDE8C8; }
.badge-new { background: #E8F8EE; color: #27AE60; border: 1px solid #C8EBD8; }
.badge-on { background: #E8F8EE; color: #27AE60; }
.badge-off { background: #FDE8E8; color: #E74C3C; }

.empty-state {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; height: 100%;
    color: var(--text-muted); text-align: center; gap: 6px;
}
.empty-state-icon { width: 48px; height: 48px; opacity: 0.6; }
.empty-state-text { font-size: 13px; font-weight: 500; }
.empty-state-sub { font-size: 11px; color: var(--text-muted); }

/* Scan section */
.scan-section {
    flex-shrink: 0;
    padding: 4px 0 calc(4px + env(safe-area-inset-bottom, 0px));
}
.box-input-row {
    display: flex; gap: 8px; margin-bottom: 10px; align-items: center;
}
.box-input {
    flex: 1; padding: 10px 14px; border: 1.5px solid var(--border);
    border-radius: var(--radius-xs); font-size: 14px;
    background: var(--card-bg); color: var(--text-primary);
    outline: none; transition: border 0.2s;
}
.box-input:focus { border-color: var(--primary); }
.save-btn {
    padding: 10px 16px; background: var(--primary); color: white;
    border: none; border-radius: var(--radius-xs); font-weight: 600;
    cursor: pointer; transition: all 0.2s;
}
.save-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.save-btn:not(:disabled):active { transform: scale(0.95); }

.action-buttons { display: flex; gap: 10px; width: 100%; }
.action-btn-item {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 5px; padding: 12px 6px; border-radius: 16px;
    border: none; cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    font-family: inherit; min-width: 0;
}
.action-btn-item:active { transform: scale(0.95); }
.action-btn-item.primary {
    background: linear-gradient(145deg, #5B7CFA, #3A56D4);
    color: #fff; box-shadow: 0 6px 18px rgba(74, 108, 247, 0.35);
}
.action-btn-item.secondary {
    background: var(--card-bg); color: var(--primary);
    border: 1.5px solid var(--primary-light);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
.action-btn-item:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.action-icon { width: 24px; height: 24px; }
.action-label { font-size: 12px; font-weight: 600; letter-spacing: 0.2px; }

/* Scanner overlay - same as original adapted */
.scanner-overlay {
    position: fixed; inset: 0; background: #0A0A0A; z-index: 100;
    display: none; flex-direction: column;
}
.scanner-overlay.active { display: flex; }
.scanner-top {
    padding: 16px 20px; display: flex; align-items: center; justify-content: space-between;
    padding-top: calc(16px + var(--safe-top)); z-index: 2;
}
.scanner-close {
    width: 44px; height: 44px; border-radius: 50%;
    border: none; background: rgba(255,255,255,0.15);
    color: #fff; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    backdrop-filter: blur(10px);
}
.scanner-top-title { color: #fff; font-size: 16px; font-weight: 600; }
.scanner-top-spacer { width: 44px; }
.scanner-body { flex: 1; display: flex; align-items: center; justify-content: center; position: relative; overflow: hidden; min-height: 0; }
.scanner-video-container { width: 100%; height: 100%; position: relative; display: flex; align-items: center; justify-content: center; }
#scannerVideoContainer { width: 100%; height: 100%; position: relative; }
#scannerVideoContainer video { width: 100% !important; height: 100% !important; object-fit: cover !important; }
.scanner-frame {
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: min(280px, 75vw); height: min(280px, 75vw); pointer-events: none; z-index: 3;
}
.scanner-frame-corner { position: absolute; width: 32px; height: 32px; border-color: #fff; border-style: solid; border-width: 0; opacity: 0.9; }
.scanner-frame-corner.tl { top: 0; left: 0; border-top-width: 4px; border-left-width: 4px; border-top-left-radius: 8px; }
.scanner-frame-corner.tr { top: 0; right: 0; border-top-width: 4px; border-right-width: 4px; border-top-right-radius: 8px; }
.scanner-frame-corner.bl { bottom: 0; left: 0; border-bottom-width: 4px; border-left-width: 4px; border-bottom-left-radius: 8px; }
.scanner-frame-corner.br { bottom: 0; right: 0; border-bottom-width: 4px; border-right-width: 4px; border-bottom-right-radius: 8px; }
.scanner-line {
    position: absolute; left: 12px; right: 12px; height: 3px;
    background: linear-gradient(90deg, transparent, #4A6CF7, #7C9AFF, #4A6CF7, transparent);
    border-radius: 2px; box-shadow: 0 0 20px #4A6CF7, 0 0 40px rgba(74,108,247,0.4);
    animation: scan-line 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}
@keyframes scan-line { 0% { top: 12px; opacity: 0.6; } 50% { top: calc(100% - 15px); opacity: 1; } 100% { top: 12px; opacity: 0.6; } }
.scanner-bottom { padding: 20px 16px calc(24px + var(--safe-bottom)); display: flex; flex-direction: column; align-items: center; gap: 12px; z-index: 2; }
.scanner-hint-text { color: rgba(255,255,255,0.85); font-size: 14px; font-weight: 500; text-align: center; }
.scanner-loading { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #fff; font-size: 15px; display: flex; flex-direction: column; align-items: center; gap: 12px; z-index: 1; }
.scanner-loading .spinner { width: 36px; height: 36px; border: 3px solid rgba(255,255,255,0.25); border-top-color: #fff; border-radius: 50%; animation: spin 0.8s linear infinite; }
.scanner-error { color: #FF6B6B; font-size: 14px; font-weight: 500; text-align: center; padding: 0 20px; display: none; position: absolute; bottom: 30px; left: 0; right: 0; z-index: 4; }
.scanner-error.show { display: block; }

/* Loading */
.loading-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.65);
    display: none; align-items: center; justify-content: center;
    flex-direction: column; gap: 16px; color: #fff; z-index: 300;
    font-size: 15px; font-weight: 500; backdrop-filter: blur(4px);
}
.loading-overlay.show { display: flex; }
.loading-overlay .spinner { width: 40px; height: 40px; border: 3px solid rgba(255,255,255,0.25); border-top-color: #fff; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Toast */
.toast {
    position: fixed; bottom: calc(140px + var(--safe-bottom)); left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: rgba(0,0,0,0.85); color: #fff;
    padding: 12px 20px; border-radius: 30px; font-size: 13px; font-weight: 500;
    z-index: 200; opacity: 0; pointer-events: none;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    backdrop-filter: blur(10px); max-width: 88vw; text-align: center; word-break: break-word;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { background: rgba(39, 174, 96, 0.92); }
.toast.error { background: rgba(255, 59, 48, 0.92); }
.toast.warning { background: rgba(230, 126, 34, 0.92); }

/* Custom Modal */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.5);
    display: none; align-items: center; justify-content: center;
    z-index: 400; backdrop-filter: blur(4px);
}
.modal-overlay.show { display: flex; }
.modal {
    background: var(--card-bg); border-radius: var(--radius);
    width: min(92vw, 720px); max-height: 85vh; overflow: hidden;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    animation: modal-in 0.25s ease-out;
    display: flex; flex-direction: column;
}
.modal.large {
    width: min(96vw, 960px); max-height: 90vh;
}
@keyframes modal-in { from { opacity: 0; transform: scale(0.9) translateY(20px); } to { opacity: 1; transform: scale(1) translateY(0); } }
.modal-header {
    padding: 16px 20px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
}
.modal-title { font-size: 16px; font-weight: 700; }
.modal-close {
    width: 32px; height: 32px; border-radius: 50%; border: none;
    background: transparent; color: var(--text-secondary); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.modal-close:hover { background: var(--primary-light); }
.modal-body { padding: 16px 20px; overflow-y: auto; flex: 1; }
.modal-footer { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }
.modal-btn {
    padding: 8px 16px; border-radius: var(--radius-xs); border: none;
    font-weight: 600; cursor: pointer; transition: all 0.2s;
}
.modal-btn.primary { background: var(--primary); color: white; }
.modal-btn.secondary { background: var(--border); color: var(--text-primary); }
.modal-btn.danger { background: var(--danger); color: white; }
.modal-btn:active { transform: scale(0.95); }

/* Admin specific */
.project-card {
    background: var(--card-bg); border-radius: var(--radius);
    padding: 16px; margin-bottom: 12px; border: 1px solid var(--border);
    box-shadow: var(--shadow);
}
.project-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.project-name { font-size: 16px; font-weight: 700; }
.status-switch {
    position: relative; width: 44px; height: 24px;
}
.status-switch input { opacity: 0; width: 0; height: 0; }
.switch-slider {
    position: absolute; cursor: pointer; inset: 0;
    background: var(--border); border-radius: 24px; transition: 0.3s;
}
.switch-slider:before {
    position: absolute; content: ""; height: 18px; width: 18px;
    left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: 0.3s;
}
input:checked + .switch-slider { background: var(--success); }
input:checked + .switch-slider:before { transform: translateX(20px); }
.project-meta { font-size: 12px; color: var(--text-secondary); margin-bottom: 10px; }
.project-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Responsive */
@media (max-width: 480px) {
    .scanner-frame { width: min(220px, 70vw); height: min(220px, 70vw); }
    .action-label { font-size: 11px; }
    .action-btn-item { padding: 10px 4px; }
}
@media (max-height: 600px) {
    .action-btn-item { padding: 8px 6px; }
}
