/* ===== CSS Variables ===== */
:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #3b82f6;
    --secondary: #64748b;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --background: #f8fafc;
    --surface: #ffffff;
    --surface-hover: #f1f5f9;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --radius: 12px;
    --radius-sm: 8px;
}

/* ===== Reset & Base ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--background);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

/* ===== App Container ===== */
.app-container {
    max-width: 100%;
    margin: 0 auto;
    min-height: 100vh;
    background: var(--surface);
    position: relative;
}

/* ===== Pages ===== */
.page { display: none; min-height: 100vh; padding-bottom: 80px; }
.page.active { display: block; }

/* ===== Header ===== */
.header {
    background: var(--primary);
    color: white;
    padding: 20px 16px;
    text-align: center;
    position: sticky;
    top: 0;
    z-index: 100;
}

.header h1 { font-size: 1.25rem; font-weight: 600; }

#order-detail-page .header { position: static; }

/* ===== Animations ===== */
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }
@keyframes pulse-red { 0%, 100% { opacity: 1; } 50% { opacity: 0.7; } }
@keyframes modalSlideIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastSlideIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }

/* ===== Loading Overlay ===== */
.loading-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.7); z-index: 9999; align-items: center; justify-content: center; flex-direction: column; }
.loading-overlay.active { display: flex; }
.loading-spinner { width: 60px; height: 60px; border: 4px solid rgba(255,255,255,0.3); border-top-color: white; border-radius: 50%; animation: spin 1s linear infinite; }
.loading-text { color: white; margin-top: 20px; font-size: 1.1rem; }

/* ===== Navigation Buttons ===== */
.back-btn {
    position: absolute; left: 16px; top: 50%; transform: translateY(-50%);
    background: transparent; border: none; color: white; font-size: 1.25rem;
    cursor: pointer; padding: 4px 8px; border-radius: var(--radius-sm);
}
.back-btn:hover { background: rgba(255, 255, 255, 0.1); }

.settings-btn {
    position: absolute; right: 16px; top: 50%; transform: translateY(-50%);
    background: transparent; border: none; color: white; font-size: 1.25rem;
    cursor: pointer; padding: 4px 8px; border-radius: var(--radius-sm);
}
.settings-btn:hover { background: rgba(255, 255, 255, 0.1); }

.export-pdf-btn {
    position: absolute; right: 16px; top: 50%; transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.2); border: none; color: white;
    font-size: 1.25rem; cursor: pointer; padding: 6px 12px;
    border-radius: var(--radius-sm); transition: all 0.2s ease;
}
.export-pdf-btn:hover { background: rgba(255, 255, 255, 0.3); transform: translateY(-50%) scale(1.05); }
.export-pdf-btn:active { transform: translateY(-50%) scale(0.95); }

/* ===== Main Content ===== */
.main-content { padding: 20px 16px; }

/* ===== Scan Section ===== */
.scan-section {
    background: var(--surface); border-radius: var(--radius);
    padding: 24px; box-shadow: var(--shadow); margin-bottom: 24px;
}

.btn-large { width: 100%; padding: 16px 24px; font-size: 1.1rem; }

.divider { display: flex; align-items: center; margin: 20px 0; color: var(--text-muted); }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.divider span { padding: 0 16px; font-size: 0.875rem; }

.manual-input { display: flex; gap: 8px; }
.manual-input .input { flex: 1; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    padding: 12px 20px; font-size: 0.95rem; font-weight: 500;
    border: none; border-radius: var(--radius-sm); cursor: pointer; transition: all 0.2s ease;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--secondary); color: white; }
.btn-secondary:hover { background: #475569; }
.btn-outline { background: transparent; color: var(--primary); border: 2px solid var(--primary); }
.btn-outline:hover { background: var(--primary); color: white; }
.btn-icon { font-size: 1.25rem; }
.btn:disabled { opacity: 0.7; cursor: not-allowed; }
.btn-spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.3); border-top-color: white; border-radius: 50%; animation: spin 0.8s linear infinite; display: inline-block; margin-right: 8px; }
.btn.loading { position: relative; pointer-events: none; }

.action-buttons { display: flex; gap: 12px; margin-top: 16px; }
.action-buttons .btn { flex: 1; }

/* ===== Inputs ===== */
.input {
    width: 100%; padding: 12px 16px; font-size: 1rem;
    border: 2px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); transition: border-color 0.2s ease;
}
.input:focus { outline: none; border-color: var(--primary); }
.input::placeholder { color: var(--text-muted); }
.input[multiple] { height: auto; min-height: 100px; overflow-y: auto; }
.input[multiple] option { padding: 4px 8px; cursor: pointer; }
.input[multiple] option:checked { background: var(--primary); color: white; }

textarea.auto-expand {
    resize: none; overflow: hidden; min-height: 44px;
    font-family: inherit; line-height: 1.5; box-sizing: border-box;
}

.input-with-sign { display: flex; gap: 8px; align-items: center; }
.input-with-sign .input { flex: 1; }
.sign-toggle-btn {
    flex-shrink: 0; width: 44px; height: 44px;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--primary);
    font-size: 1.2rem; font-weight: 600; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s, color 0.15s;
}
.sign-toggle-btn:active { background: var(--primary); color: white; }

/* ===== Recent Scans ===== */
.recent-scans { margin-bottom: 24px; }
.recent-scans h3 { font-size: 1rem; color: var(--text-secondary); margin-bottom: 12px; }
.recent-list { display: flex; flex-direction: column; gap: 8px; }
.recent-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; background: var(--surface); border-radius: var(--radius-sm);
    box-shadow: var(--shadow); cursor: pointer; transition: transform 0.2s ease;
}
.recent-item:hover { transform: translateX(4px); }
.recent-code { font-weight: 600; color: var(--primary); }
.recent-time { font-size: 0.8rem; color: var(--text-muted); }
.recent-item .sync-badge { margin-left: 8px; font-size: 0.9rem; }
.recent-sync-icon { font-size: 1.1rem; }
.recent-sync-icon.synced { color: var(--success); }
.recent-sync-icon.pending { color: var(--warning); }
.recent-retry-btn {
    background: transparent; border: none; font-size: 1rem; cursor: pointer;
    padding: 2px 6px; border-radius: 50%; transition: all 0.2s ease;
    animation: pulse-red 2s infinite;
}
.recent-retry-btn:hover { background: rgba(239, 68, 68, 0.1); transform: scale(1.1); }
.recent-retry-btn:active { transform: scale(0.95); }

.history-status { font-size: 1rem; }
.history-synced { color: var(--success); }
.history-pending { color: var(--warning); }
.history-failed { color: var(--danger); }
.history-retry-btn { background: transparent; border: none; font-size: 1rem; cursor: pointer; padding: 2px 6px; border-radius: 50%; transition: all 0.2s ease; }
.history-retry-btn:hover { background: rgba(239, 68, 68, 0.1); transform: scale(1.1); }

.recent-sync-status { margin-left: auto; font-size: 1rem; cursor: pointer; }
.recent-sync-status:hover { transform: scale(1.2); }

.empty-state { text-align: center; color: var(--text-muted); padding: 24px; font-size: 0.9rem; }

/* ===== Orders List ===== */
.orders-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 20px; }
.order-card {
    background: var(--surface); border-radius: var(--radius); padding: 16px;
    box-shadow: var(--shadow); cursor: pointer; transition: all 0.2s ease;
}
.order-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }
.order-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.order-id { font-size: 1.25rem; font-weight: 700; color: var(--text); }
.order-progress { font-size: 0.9rem; color: var(--text-secondary); }
.order-progress-bar { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; margin-bottom: 8px; }
.order-progress-fill { height: 100%; background: var(--success); transition: width 0.3s ease; }
.order-status { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 0.8rem; font-weight: 500; }
.order-status.pending { background: #fef3c7; color: #92400e; }
.order-status.in-progress { background: #dbeafe; color: #1e40af; }
.order-status.completed { background: #d1fae5; color: #065f46; }

/* ===== Search Bar ===== */
.search-bar { margin-bottom: 16px; }

/* ===== Order Detail ===== */
.order-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 24px; }
.summary-card { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); text-align: center; }
.summary-label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 4px; }
.summary-value { font-size: 1.1rem; font-weight: 600; color: var(--text); }
.status-badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 0.8rem; font-weight: 500; }

#order-detail-page .main-content { padding-left: 0 !important; padding-right: 0 !important; }
#order-detail-page .record-info { padding: 0 16px; }

.detail-header-grid { columns: 1; column-gap: 24px; }
.detail-header-grid .info-row { break-inside: avoid; }

.page-description { color: var(--text-secondary); margin-bottom: 20px; font-size: 0.95rem; }

/* ===== Rolls Summary ===== */
.rolls-summary-strip { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.rolls-summary-card { flex: 1; min-width: 80px; background: var(--surface); border-radius: var(--radius); padding: 8px 12px; text-align: center; box-shadow: var(--shadow); }
.rolls-summary-label { display: block; font-size: 0.7rem; color: var(--text-secondary); margin-bottom: 2px; text-transform: uppercase; letter-spacing: 0.4px; }
.rolls-summary-value { font-size: 1rem; font-weight: 600; color: var(--text); }
.rolls-summary-value.critical { color: red; }

/* ===== Rolls Table ===== */
.rolls-table-container {
    background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    margin-bottom: 20px; max-width: 100%; position: relative;
}
.rolls-table-container::after {
    content: ''; position: absolute; top: 0; right: 0; bottom: 0; width: 30px;
    background: linear-gradient(to right, rgba(0,0,0,0), rgba(0,0,0,0.1));
    pointer-events: none; opacity: 0; transition: opacity 0.3s;
}
.rolls-table-container.scrolled-right::after { opacity: 1; }

.rolls-table { width: 100%; border-collapse: collapse; }
.rolls-table th, .rolls-table td { padding: var(--table-cell-padding, 6px 4px); text-align: left; border-bottom: 1px solid var(--border); }
.rolls-table th {
    background: var(--surface-hover); font-size: var(--table-header-font-size, 0.72rem);
    font-weight: 600; color: var(--text-secondary); text-transform: uppercase;
    letter-spacing: 0.5px; position: sticky; top: 0; z-index: 10;
}
.rolls-table td { font-size: var(--table-body-font-size, 0.78rem); word-break: break-word; white-space: normal; vertical-align: top; }
.rolls-table th:nth-child(14), .rolls-table td:nth-child(14) { min-width: 200px; }
.rolls-table th:first-child, .rolls-table td:first-child { width: 26px; padding: 4px 2px !important; text-align: center; }
.rolls-table tr:last-child td { border-bottom: none; }
.rolls-table tr:hover td { background: var(--surface-hover); }

.roll-roll-number { font-weight: 600; color: var(--primary); }
.roll-status { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 0.75rem; font-weight: 500; }
.roll-status.pending { background: #fef3c7; color: #92400e; }
.roll-status.partial { background: #dbeafe; color: #1e40af; }
.roll-status.completed { background: #d1fae5; color: #065f46; }

.edit-roll-btn {
    background: var(--surface); border: 1px solid var(--border);
    padding: 4px 6px; border-radius: var(--radius-sm); cursor: pointer;
    font-size: var(--edit-btn-size, 0.85rem); color: var(--primary); line-height: 1;
}
.edit-roll-btn:hover { background: var(--primary); color: white; border-color: var(--primary); }

/* ===== Burger Menu ===== */
.burger-menu-item { display: block; width: 100%; padding: 14px 18px; background: none; border: none; text-align: left; font-size: 1rem; color: var(--text); cursor: pointer; }
.burger-menu-item:hover { background: var(--surface-hover); }

/* ===== Modal ===== */
.modal {
    display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.5); z-index: 1000;
    align-items: center; justify-content: center; padding: 16px;
}
.modal.active { display: flex; }
.modal-content {
    background: var(--surface); border-radius: var(--radius); width: 100%;
    max-width: 400px; max-height: 90vh; overflow-y: auto; animation: modalSlideIn 0.3s ease;
}
.modal-large { max-width: 480px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h2 { font-size: 1.1rem; font-weight: 600; }
.close-btn { background: transparent; border: none; font-size: 1.5rem; color: var(--text-muted); cursor: pointer; padding: 4px 8px; line-height: 1; }
.close-btn:hover { color: var(--text); }

/* ===== Record Form ===== */
.record-info { background: var(--surface-hover); padding: 16px 20px; border-bottom: 1px solid var(--border); }
.info-row { display: flex; align-items: baseline; gap: 4px; padding: 4px 0; flex-wrap: wrap; }
.info-label { color: var(--text-secondary); white-space: nowrap; flex-shrink: 0; }
.info-value { font-weight: 600; }
.info-value-muted { font-weight: 400; color: var(--text-muted); font-style: italic; }
.record-form { padding: 20px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.9rem; font-weight: 500; margin-bottom: 6px; color: var(--text); }
.field-hint { display: block; font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.form-actions { display: flex; gap: 12px; margin-top: 24px; }
.form-actions .btn { flex: 1; }
.form-section-title { font-size: 0.95rem; font-weight: 600; color: var(--text-secondary); margin: 20px 0 12px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ===== QR Reader ===== */
.qr-reader { width: 100%; min-height: 300px; background: var(--text); }
.qr-reader video { width: 100%; height: auto; }
.qr-reader__dashboard { padding: 10px; }
.qr-reader__dashboard_section { padding: 5px; }
.qr-reader__dashboard_section_c1 { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.qr-reader__button { padding: 8px 16px; font-size: 14px; }
.qr-reader__dashboard_section_c1 button { background: var(--primary); color: white; border: none; border-radius: 4px; padding: 8px 12px; cursor: pointer; }
.qr-reader__dashboard_section_c1 select { padding: 8px; border-radius: 4px; border: 1px solid var(--border); }
.scanner-hint { text-align: center; padding: 16px; color: var(--text-secondary); font-size: 0.9rem; }

/* ===== Combo Dropdowns ===== */
.unit-option { padding: 10px 14px; cursor: pointer; font-weight: 600; font-size: 14px; }
.unit-option:hover { background: #f1f5f9; }
.unit-option + .unit-option { border-top: 1px solid #f1f5f9; }

/* ===== Toast Notifications ===== */
.toast-container { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 2000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: white; padding: 12px 20px; border-radius: var(--radius-sm); font-size: 0.9rem; animation: toastSlideIn 0.3s ease; white-space: nowrap; }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }

/* ===== Settings Modal ===== */
.settings-group { margin-bottom: 20px; }
.settings-group label { display: block; font-size: 0.9rem; font-weight: 500; margin-bottom: 8px; color: var(--text); }
.settings-group .range-display { display: flex; justify-content: space-between; font-size: 0.85rem; color: var(--text-secondary); margin-bottom: 8px; }
.settings-group input[type="range"] { width: 100%; height: 6px; border-radius: 3px; background: var(--border); outline: none; -webkit-appearance: none; }
.settings-group input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: var(--primary); cursor: pointer; }
.settings-info { font-size: 0.8rem; color: var(--text-muted); margin-top: 8px; }

/* ===== Sync Status Indicators ===== */
.sync-indicator { font-size: 1.1rem; }
.sync-indicator.synced { color: var(--success); }
.sync-indicator.pending { color: var(--warning); }
.sync-indicator.failed { color: var(--danger); }

.sync-retry-btn { background: transparent; border: none; font-size: 1.1rem; cursor: pointer; padding: 2px 6px; border-radius: 4px; transition: all 0.2s ease; }
.sync-retry-btn:hover { background: rgba(239, 68, 68, 0.1); transform: scale(1.1); }
.sync-retry-btn:active { transform: scale(0.95); }

.sync-container { display: inline-flex; align-items: center; }

/* ===== Pending Sync Badge ===== */
.pending-sync-badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 0.75rem; font-weight: 500; background: var(--warning); color: white; }
.pending-sync-badge:empty { display: none; }

/* ===== Sync Status in Header ===== */
.sync-indicator-header { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.sync-indicator-header.online { background: var(--success); }
.sync-indicator-header.offline { background: var(--danger); }
.sync-indicator-header.syncing { background: var(--warning); animation: pulse 1s infinite; }

/* ===== Defects Text Container ===== */
.defects-text-container {
    padding: 10px 12px; border: 2px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface-hover); min-height: 48px; max-height: 200px;
    overflow-y: auto; font-size: 0.95rem; line-height: 1.5;
    color: var(--text); transition: border-color 0.2s ease;
}
.defects-text-container:empty::before { content: attr(data-placeholder); color: var(--text-muted); font-size: 0.9rem; }

/* ===== Responsive ===== */
@media (min-width: 600px) {
    .detail-header-grid { columns: 2; }
    .detail-header-full { column-span: all; }
}

@media (max-width: 480px) {
    .form-row { grid-template-columns: 1fr; }
}

@media (min-width: 768px) {
    .app-container {
        max-width: 100%; margin: 40px 2%;
        border-radius: var(--radius); box-shadow: var(--shadow-lg); min-height: auto;
    }
    .page { min-height: auto; }
    .main-content { padding: 24px 32px; }
    .scan-section { padding: 32px; }
    .btn-large { padding: 20px 32px; font-size: 1.2rem; }
    .modal-content { max-width: 560px; }
    .modal-large { max-width: 700px; }
    .record-form { padding: 24px; }
    .form-group label { font-size: 1rem; }
    .input { font-size: 1.05rem; }
}

@media (min-width: 1400px) {
    .app-container { max-width: 100%; margin: 40px 4%; }
}

@media (min-width: 1024px) and (orientation: landscape) {
    .app-container { max-width: 95%; margin: 20px auto; }
    .rolls-table-container { max-height: 70vh; overflow-y: auto; }
}
