/* AQI bulletins SQL playground: Urban Emissions styling */

:root {
    --ue-green: #164D12;
    --ue-green-dark: #0F3A0C;
    --ue-green-mid: #2F7D2A;
    --ue-green-soft: #E9F0E5;
    --bg: #F6F7F3;
    --surface: #FFFFFF;
    --border: #D6DDD0;
    --text: #1B2418;
    --text-muted: #566152;
    --danger: #B3261E;
    --danger-soft: #FBEAE8;
    --success: #1E7A1A;
    --warning: #8A5A00;

    --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --font-serif: "Source Serif 4", Georgia, "Times New Roman", serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

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

html {
    font-size: 16px;
}

body {
    font-family: var(--font-ui);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

a {
    color: var(--ue-green);
    text-underline-offset: 2px;
}

:focus-visible {
    outline: 2px solid var(--ue-green-mid);
    outline-offset: 2px;
}

/* Header */
.app-header {
    background: var(--ue-green);
    color: #fff;
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-shrink: 0;
}

.app-title h1 {
    font-family: var(--font-serif);
    font-size: 1.6rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    line-height: 1.2;
}

.app-title p {
    margin-top: 0.3rem;
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.82);
}

.app-title code {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    background: rgba(255, 255, 255, 0.14);
    padding: 0.05rem 0.4rem;
    border-radius: 4px;
    color: #fff;
}

.status-badge {
    font-size: 0.85rem;
    font-weight: 500;
    padding: 0.35rem 0.8rem;
    border-radius: 999px;
    background: #fff;
    white-space: nowrap;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
}
.status-badge.loading { color: var(--warning); }
.status-badge.error   { color: var(--danger); }
.status-badge.success { color: var(--success); }

/* Layout: Expanded sidebar width (440px) to comfortably fit 2-column checkboxes */
.app-container {
    display: grid;
    grid-template-columns: 440px minmax(0, 1fr);
    gap: 1rem;
    padding: 1rem 1.5rem;
    flex: 1;
    min-height: 0;
}

.sidebar {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#filterPanel {
    display: flex;
    flex-direction: column;
    max-height: 100%;
    padding-bottom: 0;
}

.filter-scroll-area {
    overflow-y: auto;
    padding-right: 0.25rem;
    flex: 1;
    min-height: 0;
}

.filter-footer {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 0.75rem 0 1rem;
    margin-top: 0.5rem;
    flex-shrink: 0;
}

.workspace {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-height: 0;
    min-width: 0;
}

.panel,
.query-box,
.results-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 1rem 1.1rem;
}

.panel h2,
.results-header h2 {
    font-family: var(--font-serif);
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--ue-green);
    margin-bottom: 0.25rem;
}

.hint {
    font-size: 0.88rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

/* Filter builder */
.form-group {
    margin-bottom: 0.75rem;
}

.form-group label,
.query-header label {
    display: block;
    font-size: 0.92rem;
    font-weight: 500;
    margin-bottom: 0.25rem;
}

.form-group select {
    width: 100%;
    font: inherit;
    font-size: 0.95rem;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 0.45rem 0.6rem;
}

/* Buttons */
button {
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 500;
    border-radius: 5px;
    padding: 0.5rem 0.95rem;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
}

button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.primary-btn {
    background: var(--ue-green);
    color: #fff;
    border: 1px solid var(--ue-green);
}
.primary-btn:hover:not(:disabled) { background: var(--ue-green-dark); }

.secondary-btn {
    width: 100%;
    background: var(--ue-green-soft);
    color: var(--ue-green);
    border: 1px solid var(--ue-green);
}
.secondary-btn:hover:not(:disabled) { background: #DCE8D6; }

.ghost-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}
.ghost-btn:hover:not(:disabled) { border-color: var(--ue-green); color: var(--ue-green); }

/* Ready-made questions placed above the SQL editor */
.presets-top-panel {
    padding: 0.85rem 1.1rem;
    flex-shrink: 0;
}

.presets-header-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.presets-header-row .hint {
    margin-bottom: 0.5rem;
}

.horizontal-presets {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
    gap: 0.5rem;
}

.preset-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    text-align: left;
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
}
.preset-btn:hover:not(:disabled) {
    border-color: var(--ue-green);
    background: var(--ue-green-soft);
}

/* Filter builder additions */
fieldset {
    border: none;
    min-width: 0;
}

legend,
.field-label {
    display: block;
    font-size: 0.92rem;
    font-weight: 500;
    margin-bottom: 0.25rem;
    padding: 0;
}

.field-help {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.45rem;
}

.date-row,
.two-col {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem;
}

.sub-label {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.form-group input[type="date"] {
    display: block;
    width: 100%;
    margin-top: 0.2rem;
    font: inherit;
    font-size: 0.92rem;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 0.4rem 0.5rem;
}

/* Two-column grid enabled for both categories and pollutants */
.check-list,
.check-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem 0.6rem;
}

.check {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.88rem;
    cursor: pointer;
    white-space: nowrap;
}

.check input {
    accent-color: var(--ue-green);
    width: 1rem;
    height: 1rem;
    flex-shrink: 0;
}

.check .range {
    color: var(--text-muted);
    font-size: 0.78rem;
    white-space: nowrap;
}

.check.is-disabled {
    color: var(--text-muted);
    cursor: not-allowed;
}

.match-count {
    font-size: 0.92rem;
    font-weight: 600;
    color: var(--ue-green);
    min-height: 1.35rem;
    margin-bottom: 0.45rem;
}

.match-count.warn {
    color: var(--danger);
}

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

.filter-actions .secondary-btn {
    width: auto;
    flex: 1;
    margin-top: 0;
}

/* SQL editor */
.query-box {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.query-header,
.results-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.6rem;
    flex-wrap: wrap;
}

.query-header label {
    margin-bottom: 0;
    font-family: var(--font-serif);
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--ue-green);
}

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

textarea {
    width: 100%;
    font-family: var(--font-mono);
    font-size: 0.95rem;
    line-height: 1.55;
    background: #FBFCFA;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 0.7rem 0.8rem;
    resize: vertical;
    min-height: 6.5rem;
}

textarea:focus {
    outline: none;
    border-color: var(--ue-green-mid);
    box-shadow: 0 0 0 2px rgba(47, 125, 42, 0.2);
}

textarea:disabled {
    background: #F1F3EE;
}

/* Results */
.results-box {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

.results-header h2 {
    margin-bottom: 0;
}

.results-header > div {
    display: flex;
    align-items: baseline;
    gap: 0.75rem;
    flex-wrap: wrap;
}

#resultsMeta {
    font-size: 0.9rem;
    color: var(--text-muted);
}

.table-container {
    flex: 1;
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: var(--surface);
}

.placeholder-text {
    padding: 2.5rem 1.5rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.98rem;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.93rem;
    text-align: left;
}

th, td {
    padding: 0.55rem 0.9rem;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--ue-green-soft);
    color: var(--ue-green);
    font-weight: 600;
    box-shadow: 0 1px 0 var(--border);
}

td.num,
th.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

tbody tr:hover td {
    background: #F3F7F0;
}

.null-value {
    color: var(--text-muted);
    font-style: italic;
}

.truncation-note {
    padding: 0.7rem 0.9rem;
    font-size: 0.88rem;
    color: var(--text-muted);
    background: var(--bg);
    border-top: 1px solid var(--border);
}

/* AQI category chips (CPCB colour scale) */
.chip {
    display: inline-block;
    padding: 0.1rem 0.55rem;
    border-radius: 999px;
    font-size: 0.83rem;
    font-weight: 500;
    color: #111;
    background: #E5E8E1;
}
.cat-good          { background: #00B050; color: #06210F; }
.cat-satisfactory  { background: #92D050; color: #1C2E06; }
.cat-moderate      { background: #F7D117; color: #2B2400; }
.cat-poor          { background: #FF9900; color: #2E1A00; }
.cat-very-poor     { background: #D7261E; color: #fff; }
.cat-severe        { background: #8B1A1A; color: #fff; }

/* Query progress overlay.
   SQLite runs on the page's main thread, so scripts cannot update the screen while a query
   is busy. Everything here is therefore pure CSS (transform and opacity animations), which
   browsers can keep moving on their own. */
.query-progress {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.92);
    border-radius: 6px;
    /* Stay invisible for the first 0.3 s so quick queries never flash the overlay */
    animation: progress-fade-in 0.25s ease-out 0.3s both;
}

.query-progress[hidden] {
    display: none;
}

.progress-card {
    width: min(380px, 100%);
    text-align: center;
}

.progress-track {
    height: 12px;
    border-radius: 999px;
    background: var(--ue-green-soft);
    border: 1px solid var(--border);
    overflow: hidden;
}

/* The fill uses the CPCB AQI colour scale, from Good to Severe */
.progress-fill {
    width: 40%;
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(90deg, #00B050, #92D050, #F7D117, #FF9900, #D7261E, #8B1A1A);
    animation: progress-sweep 1.3s ease-in-out infinite alternate;
}

.progress-messages {
    position: relative;
    height: 1.7rem;
    margin-top: 0.9rem;
    font-family: var(--font-serif);
    font-size: 1.1rem;
    font-weight: 500;
    color: var(--ue-green);
}

.progress-messages span {
    position: absolute;
    inset: 0;
    opacity: 0;
    animation: progress-message 12s linear infinite;
}
.progress-messages span:nth-child(2) { animation-delay: 2.4s; }
.progress-messages span:nth-child(3) { animation-delay: 4.8s; }
.progress-messages span:nth-child(4) { animation-delay: 7.2s; }
.progress-messages span:nth-child(5) { animation-delay: 9.6s; }

.progress-note {
    margin-top: 0.4rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

@keyframes progress-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* The fill is 40% of the track, so 150% of its own width reaches the far end */
@keyframes progress-sweep {
    from { transform: translateX(0); }
    to   { transform: translateX(150%); }
}

/* Five messages share a 12 s cycle, 2.4 s each */
@keyframes progress-message {
    0%   { opacity: 0; }
    2%   { opacity: 1; }
    18%  { opacity: 1; }
    20%  { opacity: 0; }
    100% { opacity: 0; }
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Errors */
.error-msg {
    margin: 1rem;
    padding: 0.9rem 1rem;
    background: var(--danger-soft);
    border-left: 4px solid var(--danger);
    border-radius: 4px;
    color: var(--text);
    font-size: 0.95rem;
}

.error-msg strong {
    color: var(--danger);
}

.error-msg ul {
    margin: 0.5rem 0 0.5rem 1.25rem;
}

.error-msg code {
    font-family: var(--font-mono);
    font-size: 0.88rem;
    background: rgba(0, 0, 0, 0.06);
    padding: 0.05rem 0.3rem;
    border-radius: 3px;
}

pre.error-msg {
    white-space: pre-wrap;
    font-family: var(--font-mono);
    font-size: 0.9rem;
}

/* Footer */
.app-footer {
    padding: 0.6rem 1.5rem 0.9rem;
    font-size: 0.85rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

/* Desktop: keep everything in one viewport */
@media (min-width: 901px) {
    body {
        height: 100vh;
        overflow: hidden;
    }
}

/* Tablet and phone: stack and let the page scroll */
@media (max-width: 900px) {
    .app-container {
        grid-template-columns: 1fr;
        padding: 1rem;
    }

    .app-header {
        flex-direction: column;
        align-items: flex-start;
        padding: 1rem;
    }

    .table-container {
        min-height: 320px;
        max-height: 70vh;
    }

    .app-footer {
        padding: 0.6rem 1rem 1rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
    }

    /* No movement: a full bar and the first message only */
    .query-progress,
    .progress-fill,
    .progress-messages span {
        animation: none;
    }
    .progress-fill { width: 100%; }
    .progress-messages span:first-child { opacity: 1; }
}