/* Maathai Portal — shared theme tokens (light + dark) */

:root,
[data-theme="light"] {
    color-scheme: light;
    --maathai-theme-color: #10b981;
    --primary-color: #10b981;
    --secondary-color: #059669;
    --primary-rgb: 16, 185, 129;

    --app-bg: #f5f7fa;
    --app-surface: #ffffff;
    --app-surface-elevated: #ffffff;
    --app-text: #1f2937;
    --app-muted: #6b7280;
    --app-border: rgba(0, 0, 0, 0.08);
    --app-border-strong: #dee2e6;
    --app-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
    --app-shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.12);

    --shell-bg: linear-gradient(160deg, #10b981 0%, #059669 100%);
    --shell-bg-topbar: linear-gradient(135deg, #10b981 0%, #059669 100%);
    --shell-text: #ffffff;
    --shell-muted: rgba(255, 255, 255, 0.75);
    --shell-border: rgba(255, 255, 255, 0.2);
    --shell-hover: rgba(255, 255, 255, 0.12);
    --shell-active: rgba(255, 255, 255, 0.22);
    --shell-active-text: #ffffff;
    --shell-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);

    --auth-primary: #10b981;
    --auth-primary-dark: #059669;
    --auth-primary-rgb: 16, 185, 129;
    --auth-bg: #f5f7fa;
    --auth-bg-gradient-end: #eef2f7;
    --auth-surface: #ffffff;
    --auth-surface-hover: #f9fafb;
    --auth-border: rgba(0, 0, 0, 0.1);
    --auth-text: #1f2937;
    --auth-text-muted: #6b7280;
    --auth-text-dim: #9ca3af;
    --auth-error: #dc2626;
    --auth-success: #059669;
    --auth-input-bg: #ffffff;
    --auth-input-icon-bg: #f3f4f6;
    --auth-input-focus-bg: #ffffff;
    --auth-footer-bg: rgba(255, 255, 255, 0.95);
    --auth-header-bg: rgba(255, 255, 255, 0.9);
    --auth-alert-danger-bg: rgba(220, 38, 38, 0.08);
    --auth-alert-danger-border: rgba(220, 38, 38, 0.25);
    --auth-alert-danger-text: #b91c1c;
    --auth-alert-success-bg: rgba(5, 150, 105, 0.08);
    --auth-alert-success-border: rgba(5, 150, 105, 0.25);
    --auth-alert-success-text: #047857;
    --auth-glow-1: rgba(16, 185, 129, 0.08);
    --auth-glow-2: rgba(16, 185, 129, 0.05);
    --auth-select-chevron: %2300000080;
    --auth-menu-bg: #ffffff;
}

[data-theme="dark"] {
    color-scheme: dark;
    --maathai-theme-color: #0c1210;
    --primary-color: #34d399;
    --secondary-color: #10b981;
    --primary-rgb: 52, 211, 153;

    --app-bg: #0f1419;
    --app-surface: #1a222d;
    --app-surface-elevated: #222b38;
    --app-text: #e5e7eb;
    --app-muted: #9ca3af;
    --app-border: rgba(255, 255, 255, 0.1);
    --app-border-strong: rgba(255, 255, 255, 0.15);
    --app-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
    --app-shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.4);

    --shell-bg: linear-gradient(180deg, #141c18 0%, #0f1419 100%);
    --shell-bg-topbar: linear-gradient(135deg, #15201b 0%, #0f1419 100%);
    --shell-text: #e5e7eb;
    --shell-muted: rgba(229, 231, 235, 0.65);
    --shell-border: rgba(52, 211, 153, 0.18);
    --shell-hover: rgba(52, 211, 153, 0.1);
    --shell-active: rgba(52, 211, 153, 0.18);
    --shell-active-text: #6ee7b7;
    --shell-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);

    --auth-bg: #0f1419;
    --auth-bg-gradient-end: #121820;
    --auth-surface: rgba(255, 255, 255, 0.06);
    --auth-surface-hover: rgba(255, 255, 255, 0.09);
    --auth-border: rgba(255, 255, 255, 0.12);
    --auth-text: #ffffff;
    --auth-text-muted: rgba(255, 255, 255, 0.6);
    --auth-text-dim: rgba(255, 255, 255, 0.45);
    --auth-error: #f87171;
    --auth-success: #4ade80;
    --auth-input-bg: rgba(255, 255, 255, 0.04);
    --auth-input-icon-bg: rgba(255, 255, 255, 0.04);
    --auth-input-focus-bg: rgba(255, 255, 255, 0.07);
    --auth-footer-bg: rgba(15, 20, 25, 0.92);
    --auth-header-bg: rgba(15, 20, 25, 0.85);
    --auth-alert-danger-bg: rgba(248, 113, 113, 0.12);
    --auth-alert-danger-border: rgba(248, 113, 113, 0.3);
    --auth-alert-danger-text: #fca5a5;
    --auth-alert-success-bg: rgba(74, 222, 128, 0.12);
    --auth-alert-success-border: rgba(74, 222, 128, 0.3);
    --auth-alert-success-text: #86efac;
    --auth-glow-1: rgba(16, 185, 129, 0.25);
    --auth-glow-2: rgba(16, 185, 129, 0.12);
    --auth-select-chevron: %23ffffff80;
    --auth-menu-bg: #1a222c;
}

/* Logo variants — only one visible; specificity must beat .app-brand-img sizing rules */
.portal-logo.portal-logo--light {
    display: none;
}

html[data-theme="light"] .portal-logo.portal-logo--dark {
    display: none;
}

html[data-theme="light"] .portal-logo.portal-logo--light {
    display: block;
}

html[data-theme="dark"] .portal-logo.portal-logo--dark {
    display: block;
}

html[data-theme="dark"] .portal-logo.portal-logo--light {
    display: none;
}

/* Theme toggle */
.portal-theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    padding: 0.45rem 0.75rem;
    border-radius: 999px;
    border: 1px solid var(--app-border-strong);
    background: var(--app-surface);
    color: var(--app-text);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.portal-theme-toggle:hover {
    background: var(--app-bg);
}

.portal-theme-toggle i {
    font-size: 0.85rem;
}

.portal-theme-toggle--on-brand {
    background: rgba(255, 255, 255, 0.14);
    border-color: rgba(255, 255, 255, 0.35);
    color: #fff;
}

.portal-theme-toggle--on-brand:hover {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
}

.portal-theme-toggle--icon-only {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
}

.portal-theme-toggle--icon-only .portal-theme-toggle-label {
    display: none;
}

.portal-theme-toggle--block {
    width: 100%;
}

.portal-theme-corner {
    position: fixed;
    top: calc(0.75rem + env(safe-area-inset-top, 0px));
    right: calc(0.75rem + env(safe-area-inset-right, 0px));
    z-index: 1100;
}

/* Portal shell dark tweaks */
[data-theme="dark"] .app-bottom-nav {
    border-top-color: var(--app-border);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] .app-sidebar .btn-outline-light {
    color: var(--shell-text);
    border-color: var(--shell-border);
}

[data-theme="dark"] .app-sidebar .btn-outline-light:hover {
    background: var(--shell-hover);
    color: var(--shell-active-text);
    border-color: rgba(52, 211, 153, 0.35);
}

[data-theme="dark"] .stat-card.success { border-left-color: #4ade80; }
[data-theme="dark"] .stat-card.info { border-left-color: #67e8f9; }
[data-theme="dark"] .stat-card.warning { border-left-color: #fbbf24; }
[data-theme="dark"] .stat-card.danger { border-left-color: #f87171; }

[data-theme="dark"] .text-primary { color: var(--primary-color) !important; }
[data-theme="dark"] .text-success { color: #4ade80 !important; }
[data-theme="dark"] .text-info { color: #67e8f9 !important; }

[data-theme="dark"] .btn-success {
    --bs-btn-bg: #059669;
    --bs-btn-border-color: #059669;
    --bs-btn-hover-bg: #047857;
    --bs-btn-hover-border-color: #047857;
}

[data-theme="dark"] .btn-outline-primary {
    --bs-btn-color: var(--primary-color);
    --bs-btn-border-color: rgba(52, 211, 153, 0.45);
    --bs-btn-hover-bg: rgba(52, 211, 153, 0.15);
    --bs-btn-hover-border-color: var(--primary-color);
    --bs-btn-hover-color: #a7f3d0;
}

[data-theme="dark"] .nav-tabs .nav-link {
    color: var(--app-muted);
}

[data-theme="dark"] .nav-tabs .nav-link.active {
    background: transparent;
    color: var(--primary-color);
    border-color: var(--app-border) var(--app-border) var(--app-surface);
}

[data-theme="dark"] .stat-card:hover {
    box-shadow: var(--app-shadow-lg);
}

[data-theme="dark"] .loading-spinner {
    border-color: #374151;
    border-top-color: var(--primary-color);
}

[data-theme="dark"] .card-no-data {
    color: var(--app-muted);
}

[data-theme="dark"] .review-item {
    border-bottom-color: var(--app-border-strong);
}

[data-theme="dark"] .nav-tabs-scroll {
    border-bottom-color: var(--app-border-strong);
}

[data-theme="dark"] .farmer-card {
    border-color: var(--app-border);
    box-shadow: var(--app-shadow);
}

[data-theme="dark"] #farmMap {
    border-color: var(--app-border-strong);
}

[data-theme="dark"] .app-account-sheet .offcanvas-body {
    background: var(--app-surface);
    color: var(--app-text);
}

[data-theme="dark"] .app-account-sheet .btn-close {
    filter: invert(1) grayscale(100%);
}

[data-theme="dark"] .app-account-sheet .portal-theme-toggle {
    background: rgba(255, 255, 255, 0.06);
    border-color: var(--app-border-strong);
    color: var(--app-text);
}

[data-theme="dark"] .modal-back-btn {
    color: var(--app-text);
}

/* Modals + forms — keep in sync with portal theme tokens */
.modal-content {
    background-color: var(--app-surface);
    color: var(--app-text);
    border-color: var(--app-border);
}

.modal-header,
.modal-footer {
    border-color: var(--app-border);
}

.modal-title {
    color: var(--app-text);
}

.modal .form-label {
    color: var(--app-text);
}

.modal .form-text {
    color: var(--app-muted);
}

.modal .form-control,
.modal .form-select {
    background-color: var(--auth-input-bg);
    border-color: var(--app-border-strong);
    color: var(--app-text);
}

.modal .form-control::placeholder {
    color: var(--auth-text-dim);
}

.modal .form-control:focus,
.modal .form-select:focus {
    background-color: var(--auth-input-focus-bg);
    border-color: var(--primary-color);
    color: var(--app-text);
    box-shadow: 0 0 0 0.2rem rgba(var(--primary-rgb), 0.2);
}

.modal .form-control:disabled,
.modal .form-select:disabled {
    background-color: var(--app-bg);
    color: var(--app-muted);
    border-color: var(--app-border);
    opacity: 1;
}

.modal .input-group .form-select,
.modal .input-group .form-control {
    background-color: var(--auth-input-bg);
    color: var(--app-text);
    border-color: var(--app-border-strong);
}

.modal .input-group-text {
    background-color: var(--auth-input-icon-bg);
    border-color: var(--app-border-strong);
    color: var(--app-muted);
}

.modal hr {
    border-color: var(--app-border);
    opacity: 1;
}

.modal code {
    color: var(--auth-success);
}

.portal-surface-box {
    background: var(--auth-input-bg);
    border: 1px solid var(--app-border-strong);
    border-radius: 0.5rem;
    color: var(--app-text);
    padding: 0.75rem 1rem;
}

.portal-inset-form {
    background: var(--auth-input-bg);
    border-color: var(--app-border-strong) !important;
    color: var(--app-text);
}

[data-theme="dark"] .modal .btn-close:not(.btn-close-white) {
    filter: invert(1) grayscale(100%);
}

[data-theme="dark"] .modal .btn-secondary {
    background-color: rgba(255, 255, 255, 0.1);
    border-color: var(--app-border-strong);
    color: var(--app-text);
}

[data-theme="dark"] .modal .btn-secondary:hover {
    background-color: rgba(255, 255, 255, 0.16);
    border-color: var(--app-border-strong);
    color: #fff;
}

[data-theme="dark"] .modal .btn-outline-secondary {
    color: var(--app-muted);
    border-color: var(--app-border-strong);
}

[data-theme="dark"] .modal .btn-outline-secondary:hover {
    background-color: rgba(255, 255, 255, 0.1);
    color: var(--app-text);
    border-color: var(--app-border-strong);
}

[data-theme="dark"] .modal .form-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%239ca3af' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3E%3C/svg%3E");
}

.modal .form-control[readonly] {
    background-color: var(--app-bg);
    color: var(--app-muted);
    opacity: 1;
}

[data-theme="dark"] .modal .btn-outline-primary {
    color: #6ee7b7;
    border-color: rgba(16, 185, 129, 0.45);
}

[data-theme="dark"] .modal .btn-outline-primary:hover {
    background-color: rgba(16, 185, 129, 0.18);
    color: #a7f3d0;
    border-color: rgba(16, 185, 129, 0.65);
}

[data-theme="dark"] .modal .btn-outline-success {
    color: #86efac;
    border-color: rgba(74, 222, 128, 0.4);
}

[data-theme="dark"] .modal .btn-outline-success:hover {
    background-color: rgba(74, 222, 128, 0.15);
    color: #bbf7d0;
}

[data-theme="dark"] .modal .btn-outline-warning {
    color: #fcd34d;
    border-color: rgba(251, 191, 36, 0.4);
}

[data-theme="dark"] .modal .btn-outline-warning:hover {
    background-color: rgba(251, 191, 36, 0.15);
    color: #fde68a;
}

[data-theme="dark"] .modal .btn-outline-danger {
    color: #fca5a5;
    border-color: rgba(248, 113, 113, 0.4);
}

[data-theme="dark"] .modal .btn-outline-danger:hover {
    background-color: rgba(248, 113, 113, 0.15);
    color: #fecaca;
}

.portal-farm-list .list-group-item {
    background-color: transparent;
    border-color: var(--app-border);
    color: var(--app-text);
}

[data-theme="dark"] .portal-farm-list .list-group-item {
    background-color: rgba(255, 255, 255, 0.03);
}

#farmMap {
    background: var(--app-bg);
}

[data-theme="dark"] .leaflet-bar a,
[data-theme="dark"] .leaflet-draw-toolbar a {
    background-color: var(--app-surface);
    border-bottom-color: var(--app-border);
    color: var(--app-text);
}

[data-theme="dark"] .leaflet-bar a:hover,
[data-theme="dark"] .leaflet-draw-toolbar a:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .leaflet-container {
    background: #1a222d;
}

[data-theme="dark"] .leaflet-control-attribution {
    background: rgba(26, 34, 45, 0.85);
    color: var(--app-muted);
}

[data-theme="dark"] .leaflet-control-attribution a {
    color: #6ee7b7;
}

[data-theme="dark"] .modal .table {
    --bs-table-bg: transparent;
    --bs-table-color: var(--app-text);
    --bs-table-border-color: var(--app-border);
    --bs-table-striped-bg: rgba(255, 255, 255, 0.03);
    --bs-table-hover-bg: rgba(255, 255, 255, 0.06);
    color: var(--app-text);
}

/* Auth light-mode background */
[data-theme="light"] body.auth-body::before {
    background:
        radial-gradient(ellipse 80% 50% at 50% -10%, var(--auth-glow-1) 0%, transparent 60%),
        radial-gradient(ellipse 60% 40% at 100% 100%, var(--auth-glow-2) 0%, transparent 50%),
        linear-gradient(180deg, var(--auth-bg) 0%, var(--auth-bg-gradient-end) 100%);
}

[data-theme="light"] .auth-alert--danger {
    color: var(--auth-alert-danger-text);
}

[data-theme="light"] .auth-alert--success {
    color: var(--auth-alert-success-text);
}

[data-theme="light"] select.auth-input {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='rgba(0,0,0,0.45)' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
}

[data-theme="dark"] select.auth-input {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='rgba(255,255,255,0.5)' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
}
