* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary: #195c49;
    --primary-dark: #134538;
    --primary-light: #23816a;
    --accent: #2a9d8f;
    --secondary: #5a6c70;
    --success: #28a745;
    --danger: #e63946;
    --warning: #f4a261;
    --bg: #f5f7f6;
    --card-bg: #ffffff;
    --text: #1a1a1a;
    --text-light: #6b7a7e;
    --border: #d4e0dc;
    --radius: 16px;
    --shadow-sm: 0 2px 8px rgba(25, 92, 73, 0.08);
    --shadow-md: 0 4px 16px rgba(25, 92, 73, 0.12);
    --shadow-lg: 0 8px 24px rgba(25, 92, 73, 0.16);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: linear-gradient(135deg, #f5f7f6 0%, #e8efed 100%);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    padding-bottom: max(80px, calc(env(safe-area-inset-bottom, 0px) + 70px));
    -webkit-text-size-adjust: 100%;
}

.container {
    max-width: 480px;
    margin: 0 auto;
    padding: 16px;
}

/* Info Notice */
.info-notice {
    display: flex;
    gap: 16px;
    align-items: flex-start;
    background: linear-gradient(135deg, #fff9e6 0%, #fff3cc 100%);
    border: 2px solid var(--warning);
    border-left: 6px solid var(--warning);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
    animation: slideIn 0.5s ease-out;
}

.info-notice.hidden {
    display: none;
}

.notice-icon {
    font-size: 2rem;
    line-height: 1;
}

.notice-content h3 {
    margin: 0 0 8px 0;
    color: #8b5e00;
    font-size: 1.1rem;
}

.notice-content p {
    margin: 0;
    color: #6b4800;
    font-size: 0.9rem;
    line-height: 1.5;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Header */
.header {
    text-align: center;
    padding: 24px 16px;
    margin-bottom: 24px;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    position: relative;
    overflow: hidden;
}

.header::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('enchantinglogo.png') no-repeat center;
    background-size: contain;
    opacity: 0.05;
    pointer-events: none;
}

.header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    color: #ffffff;
    letter-spacing: 0.5px;
    position: relative;
    margin: 0;
}

.header p {
    color: rgba(255, 255, 255, 0.9);
    font-size: 0.875rem;
    margin-top: 8px;
    position: relative;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 28px;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    touch-action: manipulation;
    position: relative;
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.btn::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    transform: translate(-50%, -50%);
    transition: width 0.6s, height 0.6s;
}

.btn:active::before {
    width: 300px;
    height: 300px;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
}

.btn-primary:hover {
    background: linear-gradient(135deg, var(--primary-light) 0%, var(--primary) 100%);
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}

.btn-secondary {
    background: var(--secondary);
    color: white;
}

.btn-secondary:hover {
    background: #495a5e;
    box-shadow: var(--shadow-md);
}

.btn-success {
    background: linear-gradient(135deg, var(--success) 0%, #218838 100%);
    color: white;
}

.btn-success:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}

.btn-danger {
    background: linear-gradient(135deg, var(--danger) 0%, #d62828 100%);
    color: white;
}

.btn-danger:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}

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

.btn-small {
    padding: 8px 16px;
    font-size: 0.85rem;
    min-height: 44px;
}

/* Mode Toggle (Online / Offline) — iOS-style segmented control */
.mode-toggle {
    display: flex;
    gap: 6px;
    margin-bottom: 16px;
    background: var(--bg);
    border: 2px solid var(--border);
    border-radius: 12px;
    padding: 4px;
}

.mode-btn {
    flex: 1;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-light);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    touch-action: manipulation;
}

.mode-btn:hover {
    color: var(--text);
}

.mode-btn.active {
    background: var(--card-bg);
    color: var(--primary);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
    font-weight: 700;
}

/* Mode Panel */
.mode-panel {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

/* Scanner Container (shared viewport per panel) */
.scanner-container {
    position: relative;
    width: 100%;
    height: 280px;
    background: #000;
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 16px;
    display: none;
    box-shadow: var(--shadow-md);
    border: 3px solid var(--primary);
}

.scanner-container.active {
    display: block;
    animation: scannerPulse 2s ease-in-out infinite;
}

@keyframes scannerPulse {
    0%, 100% { border-color: var(--primary); }
    50% { border-color: var(--accent); }
}

.viewport {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.viewport video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.viewport canvas {
    display: none;
}

.scanner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.scan-region {
    width: 80%;
    height: 120px;
    border: 4px solid var(--accent);
    border-radius: 12px;
    box-shadow: 0 0 0 9999px rgba(0,0,0,0.6),
                0 0 20px var(--accent);
    position: relative;
}

.scan-region::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--accent), transparent);
    animation: scanLineMove 2s ease-in-out infinite;
}

@keyframes scanLineMove {
    0%, 100% { transform: translateY(0); opacity: 0; }
    50% { transform: translateY(116px); opacity: 1; }
}

@keyframes scanLineSmall {
    0%, 100% { transform: translateY(0); opacity: 0; }
    50% { transform: translateY(96px); opacity: 1; }
}

/* Scanner action buttons (Batalkan / Ambil Foto) */
.scanner-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.scanner-actions .btn {
    flex: 1;
    padding: 14px 16px;
    font-size: 1rem;
}

/* OCR loading overlay (inside scanner-container while Tesseract runs) */
.ocr-loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.75);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    z-index: 2;
    border-radius: inherit;
}

.ocr-loading-overlay .loading-spinner {
    width: 44px;
    height: 44px;
    border: 4px solid rgba(255, 255, 255, 0.25);
    border-top: 4px solid #fff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.ocr-loading-overlay p {
    color: #fff;
    font-weight: 600;
    font-size: 1rem;
}

/* Scan Step */
.scan-step {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
}

.scan-step-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.step-num {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.9rem;
    flex-shrink: 0;
}

.step-title {
    font-weight: 600;
    font-size: 1rem;
    color: var(--text);
}

/* Scan Result Row */
.scan-result-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 12px;
    padding: 10px 14px;
    background: var(--card-bg);
    border: 2px solid var(--border);
    border-radius: 10px;
    transition: border-color 0.3s, background 0.3s;
}

.scan-result-row.filled {
    border-color: var(--primary);
    background: #eef6f3;
}

.scan-result-value {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-light);
    word-break: break-all;
    transition: color 0.3s;
}

.scan-result-value.has-value {
    color: var(--primary);
}

/* Scan step inner button spacing */
.scan-step > .btn-large {
    margin-bottom: 0;
}

/* Save button spacing */
#btn-save-online,
#btn-save-offline {
    margin-top: 8px;
}

/* Manual Entry */
.manual-entry {
    text-align: center;
    padding-top: 12px;
    margin-top: 12px;
    border-top: 1px solid var(--border);
}

.manual-entry p {
    color: var(--text-light);
    margin-bottom: 10px;
    font-size: 0.85rem;
}

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

.input-group input {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: 1rem;
    background: var(--card-bg);
}

.input-group input:focus {
    outline: none;
    border-color: var(--primary);
}

/* Records Section */
.records-section {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

.records-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.records-header h2 {
    font-size: 1.25rem;
}

.records-header h2 span {
    color: var(--text-light);
    font-weight: normal;
}

.records-list {
    max-height: 400px;
    overflow-y: auto;
}

.empty-state {
    text-align: center;
    color: var(--text-light);
    padding: 32px 16px;
}

/* Record Card */
.record-card {
    background: linear-gradient(135deg, #ffffff 0%, #f8fdfb 100%);
    border-radius: 12px;
    padding: 16px;
    padding-right: 34px;
    margin-bottom: 12px;
    position: relative;
    border-left: 4px solid var(--primary);
    box-shadow: var(--shadow-sm);
    transition: all 0.3s;
}

.record-card:hover {
    transform: translateX(4px);
    box-shadow: var(--shadow-md);
}

.record-card:last-child {
    margin-bottom: 0;
}

.record-card-offline {
    border-left-color: var(--warning);
}

.record-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

.record-badge-online {
    background: linear-gradient(135deg, #e8f5f2 0%, #d4ebe4 100%);
    color: var(--primary);
}

.record-badge-offline {
    background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
    color: #e65100;
}

.record-resi {
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--primary);
    margin-bottom: 8px;
    word-break: break-all;
    letter-spacing: 0.3px;
}

.record-secondary {
    font-size: 0.82rem;
    color: var(--text-light);
    margin-bottom: 4px;
}

.record-secondary strong {
    color: var(--text);
}

.record-time {
    font-size: 0.75rem;
    color: var(--text-light);
    margin-top: 8px;
}

.record-delete {
    position: absolute;
    top: 8px;
    right: 4px;
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    border-radius: 6px;
    -webkit-tap-highlight-color: transparent;
}

.record-delete:active {
    background: rgba(230, 57, 70, 0.12);
}

/* Export Bar */
.export-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top, rgba(255,255,255,0.98), rgba(255,255,255,0.95));
    backdrop-filter: blur(10px);
    padding: 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
    display: flex;
    gap: 12px;
    box-shadow: 0 -4px 20px rgba(25, 92, 73, 0.15);
    border-top: 2px solid var(--border);
    z-index: 100;
}

.export-bar .btn {
    flex: 1;
}

/* Toast */
.toast {
    position: fixed;
    bottom: max(100px, calc(env(safe-area-inset-bottom, 0px) + 80px));
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--text);
    color: white;
    padding: 14px 24px;
    border-radius: var(--radius);
    font-weight: 600;
    opacity: 0;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 1000;
    max-width: calc(100vw - 48px);
    text-align: center;
    box-shadow: var(--shadow-lg);
    backdrop-filter: blur(10px);
}

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

.toast.success {
    background: linear-gradient(135deg, var(--success) 0%, #218838 100%);
}

.toast.error {
    background: linear-gradient(135deg, var(--danger) 0%, #d62828 100%);
}

/* Responsive */
@media (min-width: 481px) {
    .container {
        padding: 24px;
    }

    .export-bar {
        max-width: 480px;
        left: 50%;
        transform: translateX(-50%);
        border-radius: var(--radius) var(--radius) 0 0;
    }
}

/* Small phones (<=380px) */
@media (max-width: 380px) {
    .container {
        padding: 10px;
    }

    .header {
        padding: 18px 12px;
        margin-bottom: 16px;
    }

    .header h1 {
        font-size: 1.5rem;
    }

    .info-notice {
        padding: 14px;
        gap: 10px;
    }

    .mode-toggle {
        padding: 3px;
        gap: 4px;
    }

    .mode-btn {
        padding: 8px 12px;
        font-size: 0.88rem;
        min-height: 40px;
        border-radius: 6px;
    }

    .mode-panel {
        padding: 16px;
    }

    .scan-step {
        padding: 12px;
        margin-bottom: 12px;
    }

    #btn-save-online,
    #btn-save-offline {
        margin-top: 16px;
    }

    .scanner-container {
        height: 220px;
    }

    .scan-region {
        height: 100px;
    }

    .scan-region::before {
        animation: scanLineSmall 2s ease-in-out infinite;
    }

    .btn-large {
        padding: 16px 20px;
        font-size: 1rem;
    }

    .input-group {
        gap: 6px;
    }

    .input-group input {
        padding: 12px 10px;
        font-size: 1rem;
    }

    .input-group .btn {
        padding: 12px 14px;
        font-size: 0.9rem;
    }

    .records-section {
        padding: 16px;
    }

    .records-header h2 {
        font-size: 1.1rem;
    }

    .export-bar .btn {
        font-size: 0.9rem;
        padding: 14px 12px;
    }
}

/* Very small phones (<=320px) */
@media (max-width: 320px) {
    .mode-toggle {
        padding: 2px;
        gap: 3px;
    }

    .mode-btn {
        padding: 6px 10px;
        font-size: 0.82rem;
        min-height: 38px;
        border-radius: 5px;
    }

    #btn-save-online,
    #btn-save-offline {
        margin-top: 20px;
    }

    .scan-result-row {
        flex-wrap: wrap;
    }

    .scan-result-value {
        flex: 1 1 100%;
        margin-bottom: 6px;
    }

    .input-group {
        flex-direction: column;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    :root {
        --primary: #23816a;
        --primary-dark: #195c49;
        --primary-light: #2a9d8f;
        --bg: #0d1f1a;
        --card-bg: #1a2e27;
        --text: #e8f5f2;
        --text-light: #8ba69c;
        --border: #2d4a3f;
        --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
    }

    body {
        background: linear-gradient(135deg, #0d1f1a 0%, #1a2e27 100%);
    }

    .info-notice {
        background: linear-gradient(135deg, #3d3416 0%, #2d2510 100%);
        border-color: var(--warning);
    }

    .notice-content h3 {
        color: #f4a261;
    }

    .notice-content p {
        color: #d4a05a;
    }

    .scan-result-row.filled {
        border-color: var(--primary);
        background: #1a3d31;
    }

    .record-card {
        background: linear-gradient(135deg, #1a2e27 0%, #1f3930 100%);
    }

    .record-badge-online {
        background: linear-gradient(135deg, #1a3d31 0%, #234536 100%);
        color: #7dd4bf;
    }

    .record-badge-offline {
        background: linear-gradient(135deg, #3d2a1a 0%, #4a3520 100%);
        color: #f4a261;
    }

    .export-bar {
        background: linear-gradient(to top, rgba(26, 46, 39, 0.98), rgba(26, 46, 39, 0.95));
        border-top-color: var(--border);
    }

    .input-group input {
        background: var(--card-bg);
    }

    .record-delete:active {
        background: rgba(230, 57, 70, 0.2);
    }
}
