/* Global Styles & Variables */
:root {
    --bg-dark: #f8fafc;
    --bg-card: #ffffff;
    --bg-card-hover: #f1f5f9;
    --border-color: #e2e8f0;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --primary-gradient: linear-gradient(135deg, #7c3aed 0%, #4f46e5 100%);
    --accent-gradient: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
    --success-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
    --font-display: 'Outfit', sans-serif;
    --shadow-premium: 0 10px 30px -10px rgba(15, 23, 42, 0.08), 0 4px 6px -4px rgba(15, 23, 42, 0.03);
    --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

body {
    background-color: var(--bg-dark);
    color: var(--text-primary);
    font-family: var(--font-sans);
    min-height: 100vh;
    overflow-x: hidden;
    position: relative;
}

/* Background Glow Effects */
.header-glow {
    position: absolute;
    top: -150px;
    right: -100px;
    width: 600px;
    height: 600px;
    background: radial-gradient(circle, rgba(124, 58, 237, 0.15) 0%, rgba(0,0,0,0) 70%);
    z-index: 0;
    pointer-events: none;
}

/* App Layout */
.app-container {
    display: flex;
    min-height: 100vh;
    position: relative;
    z-index: 1;
}

/* Sidebar Navigation */
.sidebar {
    width: 280px;
    background-color: var(--bg-card);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 2rem 1.5rem;
    position: fixed;
    height: 100vh;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 2.5rem;
}

.brand-icon {
    font-size: 2.2rem;
    background: var(--primary-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.brand-text h1 {
    font-family: var(--font-display);
    font-size: 1.4rem;
    font-weight: 800;
    letter-spacing: -0.5px;
}

.brand-text p {
    font-size: 0.75rem;
    color: #4f46e5; /* Accent Indigo brand color */
    font-weight: 700;
    letter-spacing: 1.5px;
    margin-bottom: 2px;
}

.badge-flex {
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-weight: 800;
}

/* Login Overlay (Sign in with Google) */
.login-overlay {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Dimmed backdrop — the app stays visible behind the modal */
    background-color: rgba(15, 23, 42, 0.55);
    backdrop-filter: blur(2px);
}

.login-box {
    position: relative;
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    box-shadow: var(--shadow-premium);
    padding: 3rem 3.5rem;
    text-align: center;
    max-width: 400px;
    /* Cap height and scroll if the modal (e.g. with a long notice) is taller than the
       viewport, so the bottom controls stay reachable on short screens / laptops. */
    max-height: 90vh;
    overflow-y: auto;
}

/* Close (×) button for the dismissible login modal */
.login-close-btn {
    position: absolute;
    top: 0.85rem;
    right: 0.85rem;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: var(--bg-card-hover);
    color: var(--text-secondary);
    font-size: 1rem;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.login-close-btn:hover {
    background: var(--border-color);
    color: var(--text-primary);
}

/* Registration-bonus hint inside the login modal */
.login-bonus-hint {
    font-size: 0.9rem;
    font-weight: 600;
    color: #4f46e5 !important;
    background: rgba(79, 70, 229, 0.08);
    border: 1px solid rgba(79, 70, 229, 0.18);
    border-radius: 10px;
    padding: 0.6rem 0.9rem;
    margin-bottom: 1.5rem !important;
}
.login-bonus-hint i { margin-right: 0.35rem; }

/* "Continue as guest" link-button */
.btn-guest-continue {
    display: block;
    width: 100%;
    margin-top: 1.5rem;
    padding: 0.6rem;
    border: 1px dashed var(--border-color);
    border-radius: 10px;
    background: transparent;
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.btn-guest-continue:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
}

/* Header free-trial badge (guest mode) — a friendly amber/gold accent */
.guest-trial-badge {
    background: rgba(245, 158, 11, 0.12);
    border-color: rgba(245, 158, 11, 0.3);
    color: #b45309;
}
.guest-trial-badge i { color: #f59e0b; }
.guest-trial-badge strong { color: #b45309; }

.login-box .brand-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
}

.login-box h1 {
    font-family: var(--font-display);
    font-size: 1.5rem;
    font-weight: 800;
    margin-bottom: 0.75rem;
}

.login-box p {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 1.75rem;
}

.google-signin-button {
    display: flex;
    justify-content: center;
}

.login-error {
    margin-top: 1rem;
    margin-bottom: 0 !important;
    font-size: 0.8rem;
    color: var(--danger-color) !important;
}

/* Contextual notice inside the login modal (e.g. free-trial exhausted → contact us).
   Shown in-modal so the message is never hidden behind the overlay like a toast is. */
.login-notice {
    margin-top: 0;
    margin-bottom: 1.25rem !important;
    padding: 0.75rem 0.9rem;
    font-size: 0.85rem;
    line-height: 1.6;
    text-align: left;
    color: #92400e;                 /* amber-800 */
    background-color: #fffbeb;      /* amber-50 */
    border: 1px solid #fcd34d;      /* amber-300 */
    border-radius: 10px;
    white-space: pre-line;
}

/* Notice Overlay — centered, PERSISTENT modal for quota / balance-exhausted notices.
   Reuses the login modal's centered layout; dismissed only via the ✕ button (or its
   action button). Palette follows the app theme (pure-white card, Slate message box,
   Indigo accent) — no amber. */
.notice-overlay {
    position: fixed;
    inset: 0;
    z-index: 2100;                              /* above the login overlay (2000) */
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(15, 23, 42, 0.55);   /* same dimmed backdrop as login */
    backdrop-filter: blur(2px);
}

.notice-box {
    position: relative;
    background-color: var(--bg-card);           /* pure white card */
    border: 1px solid var(--border-color);
    border-radius: 20px;
    box-shadow: var(--shadow-premium);
    padding: 2.75rem 2.5rem 2.25rem;
    text-align: center;
    max-width: 420px;
    /* Cap height and scroll for long notices (e.g. contact info) on short screens */
    max-height: 90vh;
    overflow-y: auto;
}

/* Indigo accent icon — matches the app's royal-indigo brand */
.notice-icon {
    font-size: 2.5rem;
    color: #4f46e5;
    margin-bottom: 1rem;
}

.notice-title {
    font-family: var(--font-display);
    font-size: 1.25rem;
    font-weight: 800;
    color: var(--text-primary);
    margin-bottom: 0.9rem;
}

/* Neutral Slate message box; pre-line keeps the backend's line breaks (contact info) */
.notice-message {
    font-size: 0.9rem;
    line-height: 1.7;
    text-align: left;
    color: var(--text-primary);
    background-color: var(--bg-card-hover);     /* Slate 100 */
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 0.9rem 1.1rem;
    white-space: pre-line;
}

/* Primary action button (e.g. 登入 / 註冊) — Indigo gradient */
.btn-notice-action {
    display: inline-block;
    width: 100%;
    margin-top: 1.5rem;
    padding: 0.75rem;
    border: none;
    border-radius: 10px;
    background: var(--primary-gradient);
    color: #fff;
    font-family: var(--font-sans);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.btn-notice-action:hover {
    filter: brightness(1.05);
    transform: translateY(-1px);
}

/* The top-up / contact modal is a touch wider than the shared 420px notice box, so the
   single-line "尚未開啟線上儲值功能，請聯絡專人辦理儲值。" doesn't wrap awkwardly.
   Scoped to #topup-overlay only — the quota notice keeps its original width. */
#topup-overlay .notice-box {
    max-width: 480px;
}

/* Top-up modal: amount form (shown only when ENABLE_TOPUP) */
.topup-form-section {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    text-align: left;
    margin-bottom: 0.5rem;
}
.topup-label {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
}
.topup-input {
    width: 100%;
    padding: 0.7rem 0.9rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background-color: var(--bg-card);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 0.95rem;
}
.topup-input:focus {
    outline: none;
    border-color: #4f46e5;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}
.topup-form-section .btn-notice-action {
    margin-top: 0.4rem;
}

/* Top-up modal: contact-person block (shown in BOTH states). Neutral Slate box, left
   aligned, Indigo heading — matches the app theme. */
.topup-contact {
    margin-top: 1.1rem;
    padding: 0.85rem 1rem;
    text-align: left;
    background-color: var(--bg-card-hover);   /* Slate 100 */
    border: 1px solid var(--border-color);
    border-radius: 12px;
    font-size: 0.85rem;
    line-height: 1.7;
    color: var(--text-primary);
}
.topup-contact .topup-contact-title {
    display: block;
    margin-bottom: 0.35rem;
    color: #4f46e5;                           /* Indigo accent */
    font-weight: 700;
}
.topup-contact .topup-contact-line { display: block; }

/* Dev/Test quick login */
.dev-login-section {
    margin-top: 1.5rem;
}

.dev-login-divider {
    display: flex;
    align-items: center;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.75rem;
    margin-bottom: 1rem;
}

.dev-login-divider::before,
.dev-login-divider::after {
    content: "";
    flex: 1;
    border-bottom: 1px solid var(--border-color);
}

.dev-login-divider span {
    padding: 0 0.75rem;
    white-space: nowrap;
}

.dev-login-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.btn-dev-login {
    background-color: var(--bg-dark);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.65rem 1rem;
    border-radius: 8px;
    font-family: var(--font-sans);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition-smooth);
}

.btn-dev-login:hover {
    border-color: #4f46e5;
    background-color: #f5f3ff;
    transform: translateY(-1px);
}

/* Sidebar logout button */
.user-profile {
    position: relative;
}

.btn-logout {
    margin-left: auto;
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 34px;
    height: 34px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-smooth);
}

.btn-logout:hover {
    color: var(--danger-color);
    border-color: var(--danger-color);
    background-color: rgba(239, 68, 68, 0.06);
}

/* Navigation */
.nav-menu {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    flex-grow: 1;
}

/* Sidebar promo — a single compact line nudging users toward the developer API. Sits just
   above the user profile (nav-menu's flex-grow pushes it down). Message differs by login state. */
.sidebar-promo {
    margin: 0.75rem 0 1rem;
}
.sidebar-promo-card {
    width: 100%;
    text-align: left;
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: linear-gradient(135deg, rgba(79, 70, 229, 0.08), rgba(6, 182, 212, 0.06));
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.78rem;
    line-height: 1.5;
    transition: var(--transition-smooth);
}
.sidebar-promo-card:hover {
    border-color: #4f46e5;
    color: var(--text-primary);
    box-shadow: var(--shadow-premium);
}
/* Carousel: the message fades out/in as it rotates (JS toggles .promo-fading) */
.sidebar-promo-card .promo-msg {
    display: block;
    transition: opacity 0.3s ease;
}
.sidebar-promo-card.promo-fading .promo-msg { opacity: 0; }
/* Carousel pagination dots — click to jump to a slide; active dot is highlighted */
.promo-dots {
    display: flex;
    justify-content: center;
    gap: 0.35rem;
    margin-top: 0.45rem;
}
.promo-dot {
    width: 6px;
    height: 6px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--border-color);
    cursor: pointer;
    transition: var(--transition-smooth);
}
.promo-dot:hover { background: var(--text-secondary); }
.promo-dot.is-active {
    width: 16px;
    border-radius: 3px;
    background: #4f46e5;
}

.nav-item {
    background: none;
    border: none;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.85rem 1rem;
    border-radius: 10px;
    font-family: var(--font-sans);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    transition: var(--transition-smooth);
    width: 100%;
}

.nav-item i {
    font-size: 1.1rem;
    width: 20px;
    text-align: center;
}

.nav-item:hover {
    background-color: rgba(0, 0, 0, 0.03);
    color: var(--text-primary);
}

.nav-item.active {
    background: var(--primary-gradient);
    color: white;
    box-shadow: 0 4px 15px rgba(124, 58, 237, 0.3);
}

/* Profile Info */
.user-profile {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
}

.avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--primary-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    color: white;
    font-family: var(--font-display);
    font-size: 1.2rem;
}

.user-info h4 {
    font-size: 0.9rem;
    font-weight: 600;
}

.user-info p {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Main Content Area */
.main-content {
    margin-left: 280px;
    flex-grow: 1;
    padding: 2rem 2.5rem;
    width: calc(100% - 280px);
}

/* Header Bar */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.top-bar h2 {
    font-family: var(--font-display);
    font-size: 1.8rem;
    font-weight: 700;
}

.wallet-badge {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    padding: 0.75rem 1.25rem;
    border-radius: 30px;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.9rem;
}

.wallet-badge i {
    color: #a78bfa;
    font-size: 1.1rem;
}

.wallet-badge strong {
    font-family: var(--font-display);
    font-size: 1.1rem;
    color: var(--text-primary);
}

.wallet-badge .currency {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 600;
}

/* Tab Panels */
.tab-panel {
    display: none;
    animation: fadeIn 0.4s ease-out;
}

.tab-panel.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Cards & Layout */
.card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 1.75rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--shadow-premium);
}

.card h3 {
    font-family: var(--font-display);
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.subtitle {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: -0.75rem;
    margin-bottom: 1.5rem;
}

/* Admin: per-model analysis filter (dropdown above the cost/profit table) */
.admin-model-filter {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 1rem;
}
.admin-model-filter label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
}
.admin-model-select {
    font-family: var(--font-sans);
    font-size: 0.85rem;
    color: var(--text-primary);
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.45rem 0.75rem;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.admin-model-select:hover {
    background: var(--bg-card-hover);
}
.admin-model-select:focus {
    outline: none;
    border-color: #4f46e5;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

/* Cost-analysis card header: title with a small icon-only refresh button right beside it */
.admin-cost-header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 0.6rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}
.admin-cost-header h3 { margin: 0; }
/* The .subtitle normally uses a negative top margin to hug the title; when it follows
   this flex header that pulls it up INTO the header row — cancel it here. */
.admin-cost-header + .subtitle { margin-top: 0; }

/* Log card header: title + inline refresh button (same layout as the cost card header) */
.log-card-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}
.log-card-header h3 { margin: 0; }

/* Icon-only refresh button next to the title (shared by the cost card + the log card) */
.cost-refresh-btn {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition-smooth);
}
.cost-refresh-btn:hover {
    background: var(--bg-card-hover);
    color: #4f46e5;
    border-color: #4f46e5;
}
.cost-refresh-btn:disabled { cursor: default; opacity: 0.6; }
.cost-refresh-btn.loading i { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Cost-analysis highlight: the cost columns are the focus of this table */
.data-table th.cost-col {
    color: #4f46e5;
}
.data-table td.cost-col {
    color: #4338ca;
    font-weight: 700;
    background: rgba(79, 70, 229, 0.05);
}
/* The key metric — average cost per page — gets the strongest emphasis */
.data-table td.cost-key {
    color: #4f46e5;
    font-size: 1.02rem;
    background: rgba(79, 70, 229, 0.12);
}

/* Buttons */
.btn {
    font-family: var(--font-sans);
    font-size: 0.95rem;
    font-weight: 600;
    padding: 0.8rem 1.5rem;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    transition: var(--transition-smooth);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--primary-gradient);
    color: white;
    box-shadow: 0 4px 15px rgba(124, 58, 237, 0.2);
}

.btn-primary:not(:disabled):hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(124, 58, 237, 0.4);
}

.btn-secondary {
    background-color: var(--border-color);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.btn-secondary:hover {
    background-color: var(--bg-card-hover);
}

/* Upload Layout splitting the cards and stretching to screen height */
.upload-flex-container {
    display: flex;
    gap: 1.5rem;
    align-items: stretch;
    height: calc(100vh - 160px);
    min-height: 520px;
    margin-bottom: 1.5rem;
}

.upload-area, .result-area {
    display: flex;
    flex-direction: column;
    margin-bottom: 0 !important;
    /* Allow the card to shrink to the container height so inner areas can scroll */
    min-height: 0;
}

.upload-area {
    flex: 1.2;
    /* Keep all content (drop-zone, previews, button) within the card; scroll if needed */
    overflow-y: auto;
    /* Never show a horizontal scrollbar — content wraps to fit the card width */
    overflow-x: hidden;
}

.result-area {
    flex: 1;
}

.doc-type-selector {
    margin-bottom: 1.5rem;
}

.doc-type-selector label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
}

.radio-group {
    display: grid;
    /* Wrap responsively so the options always fit the card width (no horizontal scroll) */
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 0.5rem;
}

.radio-label {
    cursor: pointer;
}

.radio-label input {
    display: none;
}

.custom-radio {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.6rem 0.5rem;
    background-color: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 500;
    transition: var(--transition-smooth);
    /* Allow the label to wrap instead of forcing horizontal overflow */
    white-space: normal;
    min-width: 0;
    text-align: center;
}

.radio-label input:checked + .custom-radio {
    border-color: #7c3aed;
    background: rgba(124, 58, 237, 0.08);
    color: var(--text-primary);
    box-shadow: 0 0 10px rgba(124, 58, 237, 0.15);
}

/* Drag and Drop Zone */
.drop-zone {
    flex-grow: 1;
    border: 2px dashed var(--border-color);
    border-radius: 12px;
    background-color: rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 2rem;
    cursor: pointer;
    transition: var(--transition-smooth);
    margin-bottom: 1.5rem;
    /* Fills the card when empty, but can shrink so previews + button still fit */
    min-height: 120px;
    flex-shrink: 1;
}

.drop-zone.drag-over {
    border-color: #7c3aed;
    background-color: rgba(124, 58, 237, 0.04);
}

.drop-icon {
    font-size: 2.5rem;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    transition: var(--transition-smooth);
}

.drop-zone:hover .drop-icon {
    color: #a78bfa;
    transform: translateY(-5px);
}

.drop-text {
    font-size: 0.95rem;
    color: var(--text-secondary);
}

.drop-text span {
    color: #a78bfa;
    font-weight: 600;
}

.file-info {
    font-size: 0.8rem;
    color: var(--success-color);
    margin-top: 0.75rem;
    font-weight: 500;
    word-break: break-all;
}

/* Result Area Components */
.result-area {
    /* Inherits card padding, borders and flex */
}

.result-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
    min-height: 38px;
}

.result-header h3 {
    margin-bottom: 0;
}

.result-actions {
    display: flex;
    gap: 0.5rem;
}

.badge {
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.35rem 0.65rem;
    border-radius: 6px;
    font-family: var(--font-display);
}

.badge-tokens {
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
}

.badge-cost {
    background-color: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.2);
    color: var(--success-color);
}
.result-placeholder {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    text-align: center;
    padding: 2rem;
    background-color: rgba(15, 23, 42, 0.02);
    border-radius: 12px;
    border: 1px solid var(--border-color);
}

.placeholder-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.4;
}

.result-placeholder .tip {
    font-size: 0.75rem;
    opacity: 0.6;
    margin-top: 0.5rem;
    max-width: 250px;
}

/* Error state: a long message (e.g. insufficient balance + contact info) should fill
   the panel width, left-aligned and comfortably spaced — not squeezed into a narrow
   centred column. Toggled via .is-error in app.js. */
.result-placeholder.is-error {
    justify-content: flex-start;
    /* Trim side padding to give the message a wider text column in the narrow result panel. */
    padding: 2.25rem 1.1rem 1.5rem;
    overflow-y: auto;
}
.result-placeholder.is-error .placeholder-icon {
    font-size: 2.2rem;
    color: var(--danger-color);
    opacity: 0.85;
}
.result-placeholder.is-error p {
    align-self: stretch;
    margin: 0 0 0.85rem;
    font-size: 0.9rem;
    line-height: 1.7;
    text-align: left;
    color: var(--danger-color);
    font-weight: 600;
    overflow-wrap: anywhere;   /* let long tokens (e.g. the email) wrap without overflowing */
    white-space: pre-line;     /* render the \n from the backend message as line breaks */
}
.result-placeholder.is-error .tip {
    align-self: stretch;
    max-width: none;
    text-align: left;
    line-height: 1.6;
}

.result-content {
    flex-grow: 1;
    background-color: #0f172a; /* Premium Slate 900 code background */
    border-radius: 12px;
    border: 1px solid var(--border-color);
    padding: 1.25rem;
    overflow-y: auto;
    min-height: 0;
    max-height: none;
}

.result-content pre {
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.85rem;
    color: #38bdf8; /* Modern terminal blue */
    white-space: pre-wrap;
    word-break: break-all;
}

/* Loading Spinners */
.loading-spinner {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
}

.double-bounce1, .double-bounce2 {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: #7c3aed;
    opacity: 0.6;
    position: absolute;
    animation: sk-bounce 2.0s infinite ease-in-out;
}

.double-bounce2 {
    animation-delay: -1.0s;
}

.loading-spinner p {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 50px;
}

@keyframes sk-bounce {
    0%, 100% { transform: scale(0.0); }
    50% { transform: scale(1.0); }
}

/* Wallet Grid & Components */
.wallet-dashboard {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.balance-card {
    background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
    border-color: rgba(255, 255, 255, 0.1);
    color: white !important;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.card-inner {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1.5rem;
}

.card-label {
    font-size: 0.85rem;
    color: #a5b4fc;
    font-weight: 500;
    margin-bottom: 0.5rem;
}

.card-balance-big {
    font-family: var(--font-display);
    font-size: 2.2rem;
    font-weight: 800;
    color: white;
}

.card-balance-big .unit {
    font-size: 1rem;
    color: #818cf8;
    font-weight: 600;
}

.card-icon-wrapper {
    background-color: rgba(124, 58, 237, 0.15);
    border: 1px solid rgba(124, 58, 237, 0.3);
    width: 50px;
    height: 50px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    color: #c084fc;
}

.topup-action h4 {
    font-size: 0.85rem;
    color: #e2e8f0; /* Soft light text */
    margin-bottom: 0.5rem;
}

.balance-card .btn-secondary {
    background-color: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
}

.balance-card .btn-secondary:hover {
    background-color: rgba(255, 255, 255, 0.25);
}

.topup-input-group {
    display: flex;
    gap: 0.5rem;
}

.topup-input-group input {
    background-color: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 0.6rem 1rem;
    border-radius: 8px;
    width: 100px;
    font-size: 0.9rem;
    outline: none;
    transition: var(--transition-smooth);
}

.topup-input-group input:focus {
    border-color: white;
    background-color: rgba(255, 255, 255, 0.15);
}

.topup-input-group input::placeholder {
    color: rgba(255, 255, 255, 0.5);
}

.stats-summary-card {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.pricing-info {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin: 1rem 0;
}

.price-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
}

.price-badge {
    padding: 0.25rem 0.5rem;
    border-radius: 5px;
    font-size: 0.75rem;
    font-weight: 600;
}

.price-badge.input-badge {
    background-color: rgba(124, 58, 237, 0.08);
    color: #7c3aed;
}

.price-badge.output-badge {
    background-color: rgba(6, 182, 212, 0.08);
    color: #0891b2;
}

.price-value {
    font-family: var(--font-display);
    font-weight: 600;
    color: var(--text-primary);
}

.disclaimer {
    font-size: 0.75rem;
    color: var(--text-secondary);
    background-color: rgba(245, 158, 11, 0.05);
    border: 1px solid rgba(245, 158, 11, 0.15);
    padding: 0.75rem;
    border-radius: 8px;
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
}

.disclaimer i {
    color: var(--warning-color);
    margin-top: 2px;
}

/* Data Tables */
.table-container {
    overflow-x: auto;
    width: 100%;
    margin-top: 0.5rem;
}

/* Personal recognition log: cap the height and scroll INSIDE the table so a long history
   doesn't stretch the page (leaves room for the report section planned below it). Scoped to
   .log-card so the API-key / admin tables are unaffected. */
.log-card .table-container {
    max-height: 420px;
    overflow-y: auto;
}
/* Keep the column headers visible while the body scrolls (opaque bg so rows don't show through) */
.log-card .table-container thead th {
    position: sticky;
    top: 0;
    background-color: var(--bg-card);
    z-index: 1;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

.data-table th, .data-table td {
    padding: 1rem;
    font-size: 0.85rem;
    border-bottom: 1px solid var(--border-color);
}

.data-table th {
    color: var(--text-secondary);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.data-table tbody tr:hover {
    background-color: rgba(0, 0, 0, 0.01);
}

.empty-table {
    text-align: center;
    color: var(--text-secondary);
    padding: 2.5rem !important;
}

/* Per-key cost report: period controls + total row */
.cost-report-controls {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.6rem;
    margin: 0.25rem 0 1rem;
}
.cost-report-controls label { font-size: 0.85rem; color: var(--text-secondary); font-weight: 600; }
.cost-report-controls select,
.cost-custom-range input[type="date"] {
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 0.85rem;
}
.cost-custom-range { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.cost-range-label { font-size: 0.8rem; color: var(--text-secondary); }

/* Chart area */
.cost-chart-wrap {
    position: relative;
    height: 340px;
    margin-top: 0.5rem;
}

/* Source multi-select (checkbox dropdown) */
.cost-source-filter { position: relative; }
.cost-source-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.7rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.cost-source-btn:hover { background: var(--bg-card-hover); border-color: #4f46e5; color: #4f46e5; }
.cost-source-menu {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    z-index: 20;
    min-width: 180px;
    max-height: 260px;
    overflow-y: auto;
    padding: 0.4rem;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: var(--shadow-premium);
}
.cost-source-menu label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.5rem;
    border-radius: 6px;
    font-size: 0.83rem;
    color: var(--text-primary);
    cursor: pointer;
}
.cost-source-menu label:hover { background: var(--bg-card-hover); }
.cost-source-menu .cost-source-swatch {
    width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0;
}

/* Recognition-count cell (成功/失敗/總): colour-coded for quick scanning */
.count-cell {
    white-space: nowrap;
    font-variant-numeric: tabular-nums;   /* aligned digit widths */
}
.count-cell .cnt-success { color: var(--success-color); font-weight: 600; }
.count-cell .cnt-failed  { color: var(--danger-color);  font-weight: 600; }
.count-cell .cnt-zero    { color: var(--text-secondary); }   /* 0 failures — muted */
.count-cell .cnt-total   { color: var(--text-primary);  font-weight: 700; }
.count-cell .cnt-sep     { color: var(--text-secondary); margin: 0 0.3rem; }
/* Centre these columns (header + cells): 辨識張數, plus 來源 IP / 狀態 via .col-center */
.data-table th.col-count,
.data-table td.col-count,
.data-table th.col-center,
.data-table td.col-center { text-align: center; }
.data-table th.col-right,
.data-table td.col-right { text-align: right; }

/* Money ledger (admin 金流紀錄): scroll inside the table + sticky header + amount colours */
.admin-tx-card .table-container {
    max-height: 460px;
    overflow-y: auto;
}
.admin-tx-card .table-container thead th {
    position: sticky;
    top: 0;
    background-color: var(--bg-card);
    z-index: 1;
}
.tx-amount-pos { color: #10b981; font-weight: 700; }   /* credit (加值) — green */
.tx-amount-neg { color: #ef4444; font-weight: 700; }   /* debit (扣款) — red */
.tx-channel {
    display: inline-block;
    font-size: 0.72rem;
    padding: 0.12rem 0.55rem;
    border-radius: 999px;
    background-color: var(--bg-card-hover);
    color: var(--text-secondary);
    white-space: nowrap;
}
.tx-src-web { color: var(--text-secondary); }          /* web-console deductions look quieter */

/* Eye toggle (顯示已註銷的金鑰) — shared by the API-keys tab and the cost report */
.eye-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.35rem 0.7rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 0.8rem;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.eye-toggle:hover { border-color: #4f46e5; color: var(--text-primary); }
.eye-toggle.is-on { border-color: #4f46e5; color: #4f46e5; background: #f5f3ff; }

.apikeys-toolbar {
    display: flex;
    justify-content: flex-end;
    margin-top: 1rem;
}

/* Revoked API-key row: greyed-out / de-emphasised */
.apikey-row-revoked { opacity: 0.55; }
.apikey-row-revoked code { color: var(--text-secondary); }
.revoked-badge {
    display: inline-block;
    font-size: 0.72rem;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    background-color: var(--bg-card-hover);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    white-space: nowrap;
}

/* Status Badges */
.status-badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    display: inline-block;
}

.status-badge.success {
    background-color: rgba(16, 185, 129, 0.08);
    color: var(--success-color);
}

.status-badge.failed {
    background-color: rgba(239, 68, 68, 0.08);
    color: var(--danger-color);
}

.status-badge.pending {
    background-color: rgba(245, 158, 11, 0.08);
    color: var(--warning-color);
}

/* Batch partially succeeded (some units OK, some failed) — Indigo accent */
.status-badge.partial {
    background-color: rgba(79, 70, 229, 0.08);
    color: #4f46e5;
}

.text-success { color: var(--success-color); }
.text-danger { color: var(--danger-color); }

/* Admin Dashboard Cards & Charts */
.admin-metrics-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.25rem;
    margin-bottom: 1.5rem;
}

.metric-card {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.25rem;
}

.metric-card h3 {
    margin-bottom: 0;
    font-family: var(--font-display);
    font-size: 1.4rem;
    font-weight: 800;
}

.metric-card p {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 0.2rem;
}

.metric-icon {
    width: 48px;
    height: 48px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
}

.metric-icon.blue { background: rgba(59, 130, 246, 0.08); color: #2563eb; }
.metric-icon.green { background: rgba(16, 185, 129, 0.08); color: #059669; }
.metric-icon.purple { background: rgba(139, 92, 246, 0.08); color: #7c3aed; }
.metric-icon.gold { background: rgba(245, 158, 11, 0.08); color: #d97706; }

.charts-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.chart-card {
    min-height: 350px;
}

.chart-wrapper {
    position: relative;
    height: 250px;
    width: 100%;
}

/* Persistent notice in the result panel (e.g. free-trial truncated to the first image).
   Toggled to display:flex in app.js; stays until the next recognition (unlike a toast). */
.result-notice {
    align-items: flex-start;
    gap: 0.5rem;
    margin: 0 0 1rem;
    padding: 0.75rem 1rem;
    font-size: 0.85rem;
    line-height: 1.6;
    color: #92400e;                 /* amber-800 */
    background-color: #fffbeb;      /* amber-50 */
    border: 1px solid #fcd34d;      /* amber-300 */
    border-left: 4px solid #f59e0b; /* amber-500 accent */
    border-radius: 8px;
}
.result-notice i {
    margin-top: 0.15rem;
    color: #f59e0b;
}

/* Toast Messages */
.toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    padding: 1rem 1.5rem;
    border-radius: 10px;
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 25px rgba(15, 23, 42, 0.08);
    /* Above the login overlay (z-index 2000) so toasts are never hidden behind the modal. */
    z-index: 2500;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.9rem;
    transform: translateY(100px);
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.toast.show {
    transform: translateY(0);
    opacity: 1;
}

.toast.success-toast {
    border-color: rgba(16, 185, 129, 0.3);
    color: var(--success-color);
}

.toast.error-toast {
    border-color: rgba(239, 68, 68, 0.3);
    color: var(--danger-color);
}

.toast.info-toast {
    border-color: rgba(59, 130, 246, 0.3);
    color: #2563eb;
}

/* Table Management Actions */
.btn-small {
    font-size: 0.75rem;
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
}

/* Admin 儲值 modal — a slightly wider card than the shared notice box */
#admin-topup-overlay .notice-box { max-width: 440px; }

/* Small caption above the customer info block */
.admin-topup-caption {
    font-size: 0.82rem;
    color: var(--text-secondary);
    text-align: left;
    margin-bottom: 0.6rem;
}

/* Read-only customer info block — soft light-gray fill, gentle rounding, no strong border.
   STRICT two-column grid so every value starts at the same x (col 2), never ragged. */
.admin-topup-user {
    display: grid;
    grid-template-columns: auto 1fr;   /* col1 = widest key, col2 = values (aligned) */
    align-items: center;
    width: 100%;
    margin-bottom: 1.5rem;
    padding: 0.25rem 1rem;
    background-color: var(--bg-card-hover);   /* Slate 50/100 — unified light-gray fill */
    border-radius: 12px;
}
.admin-topup-user-key,
.admin-topup-user-val {
    padding: 0.7rem 0;
    font-size: 0.88rem;                 /* balance row is the SAME size as the others now */
}
.admin-topup-user-key {
    font-weight: 700;                   /* keys: bold */
    color: var(--text-secondary);
    padding-right: 1.25rem;             /* gutter between the two columns */
    white-space: nowrap;
}
.admin-topup-user-val {
    font-weight: 400;                   /* values: NOT bold */
    color: var(--text-primary);
    text-align: left;
    word-break: break-all;
}
/* Continuous inner dividers between rows (skip the last row); no column gap → line is unbroken */
.admin-topup-user > :nth-child(-n+4) {
    border-bottom: 1px solid var(--border-color);
}
/* Current balance value: stands out via an accent colour only (stays non-bold, per spec) */
.admin-topup-user-val--balance {
    color: #4f46e5;
}

/* Amount input area — left-aligned label + helper, add-value only */
.admin-topup-form {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    text-align: left;
}
.admin-topup-label {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-align: left;
}
.admin-topup-form .btn-notice-action { margin-top: 0.75rem; }

/* Result Area Sub-Tabs styling */
.result-tab-bar {
    display: flex;
    background-color: rgba(0, 0, 0, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.25rem;
    margin-bottom: 1.25rem;
    gap: 0.25rem;
}

.res-tab {
    flex: 1;
    background: none;
    border: none;
    padding: 0.5rem 0.75rem;
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: var(--transition-smooth);
}

.res-tab:hover {
    color: var(--text-primary);
    background-color: rgba(0, 0, 0, 0.02);
}

.res-tab.active {
    background-color: var(--bg-card);
    color: var(--text-primary);
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
    border: 1px solid rgba(15, 23, 42, 0.02);
}

.res-panel {
    display: none;
    flex-direction: column;
    flex-grow: 1;
    min-height: 0;
}

.res-panel.active {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* Floating "複製" button for the JSON result — top-right of the JSON panel, over the dark
   code area. Stays put while the code scrolls (anchored to the panel, not the content). */
#json-panel { position: relative; }
.json-copy-btn {
    position: absolute;
    top: 0.6rem;
    right: 0.6rem;
    z-index: 2;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.3rem 0.7rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background-color: var(--bg-card);
    color: #4f46e5;               /* Indigo accent */
    font-family: var(--font-sans);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition-smooth);
    opacity: 0.92;
}
.json-copy-btn:hover {
    background-color: var(--bg-card-hover);
    opacity: 1;
}

.result-table-wrapper {
    flex-grow: 1;
    background-color: var(--bg-card);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    padding: 1.25rem;
    overflow-y: auto;
    min-height: 0;
    max-height: none;
}

/* Per-image result selector (paid batch, >1 image) — chips pick which image's
   structured result to view; palette follows the app theme (Slate / Indigo, no amber). */
.result-selector {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.85rem;
    flex-shrink: 0;
}
.result-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    max-width: 220px;
    padding: 0.4rem 0.8rem;
    border: 1px solid var(--border-color);
    border-radius: 999px;
    background-color: var(--bg-card);
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition-smooth);
}
.result-chip:hover {
    background-color: var(--bg-card-hover);
    color: var(--text-primary);
}
/* Selected image — Indigo accent */
.result-chip.active {
    border-color: #4f46e5;
    background-color: rgba(79, 70, 229, 0.08);
    color: #4f46e5;
}
/* Failed image — muted, with a danger-tinted status icon */
.result-chip.failed {
    color: var(--text-secondary);
    border-style: dashed;
}
.result-chip .chip-status-ok { color: var(--success-color); }
.result-chip .chip-status-fail { color: var(--danger-color); }
.result-chip .chip-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
/* Inline message shown when a FAILED image chip is selected */
.result-unit-failed {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 1rem 1.1rem;
    color: var(--danger-color);
    font-size: 0.88rem;
    line-height: 1.6;
}

/* Table OCR structure formatting */
.table-ocr-details {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1.25rem;
}

.table-ocr-details tr {
    border-bottom: 1px solid var(--border-color);
}

.table-ocr-details tr:last-child {
    border-bottom: none;
}

.table-ocr-details td {
    padding: 0.75rem 0.5rem;
    font-size: 0.85rem;
    vertical-align: top;
}

.table-ocr-details td.key {
    color: #4f46e5; /* Royal indigo for keys in light theme */
    font-weight: 600;
    width: 32%;
}

.table-ocr-details td.value {
    color: var(--text-primary);
    word-break: break-all;
}

/* Table nested OCR items list */
.ocr-items-header {
    font-family: var(--font-display);
    font-size: 0.95rem;
    font-weight: 600;
    margin-top: 1rem;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.table-ocr-items {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    margin-top: 0.5rem;
}

.table-ocr-items th {
    background-color: var(--bg-dark);
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.6rem 0.75rem;
    text-transform: uppercase;
    border-bottom: 1px solid var(--border-color);
}

.table-ocr-items td {
    padding: 0.6rem 0.75rem;
    font-size: 0.8rem;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
}

.table-ocr-items tr:last-child td {
    border-bottom: none;
}

/* --- 收據 (receipt) 結果專用微調；只作用在 .receipt-view，不影響其他文件類型 --- */
/* ① 整張表做成圓角外框（同 ② 的 .table-ocr-items），這樣頂端的表頭就會有圓角弧度 */
.receipt-view .table-ocr-details {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
}
/* ① 項目名稱 / 項目內容 表頭：加粗、底色 #efedff、靠左、字級同「費用項目」(0.95rem) */
.receipt-view .table-ocr-details thead th {
    background-color: #efedff;
    color: var(--text-primary);
    font-weight: 700;
    text-align: left;
    padding: 0.75rem 0.5rem;
    font-size: 0.95rem;
}
/* ② key（醫院名稱、病患姓名 等）：與 value 同色，且不加粗 */
.receipt-view .table-ocr-details td.key {
    color: var(--text-primary);
    font-weight: 400;
}
/* ③ 「費用項目」標題用靛藍色 */
.receipt-view .ocr-items-header {
    color: #4f46e5;
}
/* ④ 費用項目子表表頭（項目名稱 / 自付金額）：底色 #efedff、加粗、靠左、字級同「費用項目」 */
.receipt-view .table-ocr-items th {
    background-color: #efedff;
    color: var(--text-primary);
    text-align: left;
    font-weight: 700;
    font-size: 0.95rem;
}

/* --- Generic nested renderer (complex types, render="nested"): indented tree --- */
.nested-obj {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

/* Scalar "key  value" row */
.nested-row {
    display: flex;
    gap: 0.75rem;
    font-size: 0.85rem;
    line-height: 1.5;
}

.nested-row .nested-key {
    color: #4f46e5; /* Royal indigo, matches the field table */
    font-weight: 600;
    flex: 0 0 8em;      /* fixed key column keeps values aligned */
    max-width: 40%;
}

.nested-row .nested-val {
    color: var(--text-primary);
    flex: 1 1 auto;
    overflow-wrap: anywhere;  /* wrap long tokens gracefully, never char-by-char in a sliver */
    white-space: pre-wrap;    /* keep readable spacing in dates/sentences */
}

/* A nested object/array: a label with its content indented full-width underneath */
.nested-block {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.nested-block-key {
    color: #4f46e5;
    font-weight: 600;
    font-size: 0.85rem;
}

.nested-block-body {
    margin-left: 0.5rem;
    padding-left: 0.85rem;
    border-left: 2px solid var(--border-color);
}

.nested-block-body > .table-ocr-items {
    margin-top: 0.25rem;
}

.nested-empty {
    color: var(--text-secondary);
    opacity: 0.7;
}

/* Long text value (e.g. 全文辨識) — a full-width block instead of a cramped 2-column row.
   Label + 複製 button on top; the text spans the whole panel, pre-wrap keeps the original
   line breaks, and a bounded height scrolls a huge OCR dump instead of pushing the page. */
.nested-longtext-block {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}
.nested-longtext {
    color: var(--text-primary);
    background-color: var(--bg-card-hover);   /* Slate 100 */
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 0.85rem 1rem;
    font-size: 0.85rem;
    line-height: 1.75;
    white-space: pre-wrap;        /* keep the OCR's own line breaks */
    overflow-wrap: break-word;    /* break long tokens at word boundaries, not char-by-char */
    /* NO inner max-height / scroll: the block grows to fit and the結構化表格 wrapper
       (.result-table-wrapper, overflow-y:auto) does the single scroll — an inner cap here
       would add a SECOND nested scrollbar. */
}

/* Quick Topup Button in Header */
/* Quick Topup Button in Header - Light CTA */
.topup-pill-btn {
    background-color: var(--bg-card) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05) !important;
    cursor: pointer;
    font-weight: 600;
    font-family: var(--font-sans);
    transition: all 0.2s ease;
}

.topup-pill-btn i {
    color: #4f46e5 !important;
    font-size: 1rem;
}

.topup-pill-btn:hover {
    transform: translateY(-1px);
    border-color: #4f46e5 !important;
    background-color: #f5f3ff !important;
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.08) !important;
}

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

/* Thumbnail Preview Layout */
.preview-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1.5rem;
    padding: 0.75rem;
    background-color: var(--bg-dark);
    border-radius: 8px;
    border: 1px solid var(--border-color);
    /* Bound the preview area so many files scroll here instead of pushing the button out */
    max-height: 200px;
    overflow-y: auto;
    flex-shrink: 0;
}

.preview-item {
    position: relative;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 0.4rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    text-align: center;
    box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.preview-thumbnail {
    width: 90px;
    height: 70px;
    object-fit: cover;
    border-radius: 4px;
    border: 1px solid var(--border-color);
}

.preview-pdf-icon {
    width: 90px;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fef2f2;
    color: #ef4444;
    font-size: 2rem;
    border-radius: 4px;
    border: 1px solid #fee2e2;
}

.preview-name {
    font-size: 0.7rem;
    color: var(--text-primary);
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
}

.preview-remove-btn {
    position: absolute;
    top: -5px;
    right: -5px;
    background-color: #ef4444;
    color: white;
    border: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    transition: all 0.2s ease;
}

.preview-remove-btn:hover {
    background-color: #dc2626;
    transform: scale(1.1);
}

/* ==========================================================================
   Layout redesign: collapsible sidebar, image viewer, resizable panels
   ========================================================================== */

/* --- Collapsible sidebar --- */
.sidebar {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.main-content {
    transition: margin-left 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.app-container.sidebar-collapsed .sidebar {
    transform: translateX(-100%);
}
.app-container.sidebar-collapsed .main-content {
    margin-left: 0;
    width: 100%;
}

.top-bar-left {
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: margin-left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
/* When the sidebar is collapsed, shift the title right so the floating ☰ doesn't cover it */
.app-container.sidebar-collapsed .top-bar-left {
    margin-left: 3.25rem;
}

/* Collapse button inside the sidebar (top-right of the brand row) */
.sidebar-collapse-btn {
    margin-left: auto;
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 34px;
    height: 34px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-smooth);
    flex-shrink: 0;
}
.sidebar-collapse-btn:hover {
    border-color: #4f46e5;
    color: #4f46e5;
    background-color: #f5f3ff;
}

/* Floating button to re-open the sidebar — only shown when collapsed */
.sidebar-open-btn {
    position: fixed;
    top: 1.75rem;
    left: 1rem;
    z-index: 60;
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    width: 42px;
    height: 42px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 1.1rem;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-premium);
    transition: var(--transition-smooth);
}
.sidebar-open-btn:hover {
    border-color: #4f46e5;
    color: #4f46e5;
    background-color: #f5f3ff;
}
.app-container.sidebar-collapsed .sidebar-open-btn {
    display: flex;
}

/* --- Resizable panels --- */
/* Result panel defaults to ~30% of the width; upload area takes the rest */
.upload-area { flex: 1 1 auto; }
.result-area { flex: 0 0 30%; }

.panel-resizer {
    flex: 0 0 6px;
    align-self: stretch;
    cursor: col-resize;
    border-radius: 4px;
    background-color: var(--border-color);
    position: relative;
    transition: background-color 0.2s;
}
.panel-resizer:hover,
.panel-resizer.dragging {
    background-color: #7c3aed;
}
.panel-resizer::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 2px;
    height: 32px;
    background-color: rgba(148, 163, 184, 0.6);
    border-radius: 2px;
}

/* --- Image viewer --- */
.image-viewer {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    /* Guarantees a tall image area; the upload card scrolls to reveal all of it */
    min-height: 440px;
    margin-bottom: 1.5rem;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
    background-color: #0f172a;
}
/* When files are loaded, shrink the drop-zone to a slim bar so the viewer dominates */
.upload-area.has-files .drop-zone {
    flex: 0 0 auto;
    min-height: 0;
    padding: 0.6rem 1rem;
    margin-bottom: 1rem;
}
.upload-area.has-files .drop-icon {
    display: none;
}

.viewer-toolbar {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.75rem;
    background-color: #1e293b;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    flex-shrink: 0;
}
.viewer-btn {
    background-color: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #e2e8f0;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    transition: var(--transition-smooth);
}
.viewer-btn:hover {
    background-color: #7c3aed;
    border-color: #7c3aed;
    color: white;
}
.viewer-btn-text {
    width: auto;
    padding: 0 0.85rem;
    gap: 0.4rem;
    font-size: 0.8rem;
    font-weight: 600;
    white-space: nowrap;
}
.viewer-zoom-label {
    color: #cbd5e1;
    font-size: 0.8rem;
    font-weight: 600;
    min-width: 52px;
    text-align: center;
    font-family: var(--font-display);
}
.viewer-divider {
    width: 1px;
    height: 22px;
    background-color: rgba(255, 255, 255, 0.15);
    margin: 0 0.25rem;
}
.viewer-stage {
    flex: 1 1 auto;
    /* Fills the viewer height; the viewer itself carries the min-height */
    min-height: 0;
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background-image: repeating-conic-gradient(#182234 0% 25%, #0f172a 0% 50%);
    background-size: 24px 24px;
}
#viewer-img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    transform-origin: center center;
    user-select: none;
    -webkit-user-drag: none;
    cursor: grab;
    transition: transform 0.08s linear;
}
#viewer-img.grabbing {
    cursor: grabbing;
    transition: none;
}
.viewer-pdf {
    color: #94a3b8;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
    text-align: center;
    padding: 1rem;
}
.viewer-pdf i {
    font-size: 3rem;
    color: #ef4444;
}
.viewer-thumbs {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem;
    background-color: #1e293b;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    overflow-x: auto;
    flex-shrink: 0;
}
.viewer-thumb {
    position: relative;
    flex: 0 0 auto;
    width: 58px;
    height: 44px;
    border-radius: 6px;
    border: 2px solid transparent;
    overflow: hidden;
    cursor: pointer;
    background-color: #0f172a;
    transition: border-color 0.2s;
}
.viewer-thumb.active {
    border-color: #7c3aed;
}
.viewer-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.viewer-thumb .thumb-pdf {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ef4444;
}
.viewer-thumb-remove {
    position: absolute;
    top: 0;
    right: 0;
    width: 16px;
    height: 16px;
    border-radius: 0 0 0 6px;
    background-color: #ef4444;
    color: white;
    border: none;
    font-size: 0.6rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
}
.viewer-thumb:hover .viewer-thumb-remove {
    opacity: 1;
}

