/*
 * Home.theme.css - light/dark theming for the task pane.
 *
 * Must be the LAST stylesheet in Home.html / index.html. style.css and
 * twelvedata.com's screen.css set colours on these same selectors, so anything
 * loaded earlier loses at equal specificity.
 *
 * The problem it fixes: style.css gives body a colour (#111922) but no
 * background-color. When Office renders the pane on a dark backdrop the page
 * stays transparent, so every label, input value and button caption is
 * near-black on near-black. Meanwhile .bottom__form-wrapper (#f7f8fb),
 * .popup and .error (#fff) stay light, so the pane ends up half-and-half.
 *
 * Both palettes are therefore declared explicitly. The light values are pinned
 * to exactly what the pane already looked like - including select2's own
 * defaults (#aaa border, #888 arrow, #ddd selected row) - so light mode is
 * unchanged. Dark mode is the new part.
 *
 * Dark applies on prefers-color-scheme, and also on :root[data-theme="dark"]
 * so it can be forced from JS (e.g. Office.context.officeTheme) if some host
 * build turns out not to report a colour scheme to the webview.
 */

:root {
    color-scheme: light;

    --td-bg: #ffffff;           /* page */
    --td-bg-raised: #f7f8fb;    /* the API-key strip at the bottom */
    --td-surface: #ffffff;      /* support popup, error panel */
    --td-fg: #111922;
    --td-fg-base: #333333;      /* what .ms-Fabric puts on <body>; see below */
    --td-fg-label: #111922;     /* the <span> above each field */
    --td-rule: #edf0f5;         /* .main__top divider */
    --td-mark: #aeb9c7;         /* the 12x32 block at the top-right of .main__top */
    --td-link: #2469f0;

    /* Corner rounding for every field and button. style.css already uses 4px on
       the native inputs and the buttons, and select2 uses 4px on its own boxes,
       so this is the existing look expressed as one knob. Not a colour - it is
       declared once here and is not repeated in the dark palettes. */
    --td-radius: 4px;

    --td-input-bg: #ffffff;
    --td-input-fg: #111922;
    --td-input-border: #aeb9c7;

    --td-select-bg: #f7f8fb;    /* the closed box - style.css's own value, not select2's #fff */
    --td-select-panel: #ffffff; /* the open dropdown and its search field */
    --td-select-border: #aaaaaa;
    --td-select-fg: #444444;    /* the chosen value, shown in the closed box */
    --td-select-ph: #999999;
    --td-select-arrow: #888888;
    --td-select-sel: #dddddd;   /* already-selected row */

    --td-btn-fg: #111922;
    --td-btn-border: #111922;

    --td-overlay: #111922;      /* .inactive-bg, rendered at opacity .3 */
    --td-icon-filter: none;     /* closebtn-icon.svg is stroked #111922 */
}

/* Dark palette. Kept in sync with the :root[data-theme="dark"] block below -
   CSS has no way to share one declaration between a media query and a plain
   selector, and there is no build step here to do it for us. */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;

        --td-bg: #161d26;
        --td-bg-raised: #1e2731;
        --td-surface: #1e2731;
        --td-fg: #e9edf3;
        --td-fg-base: #e9edf3;
        --td-fg-label: #a7b2c0;
        --td-rule: #2b3542;
        --td-mark: #3a4655;
        --td-link: #6fa3ff;

        --td-input-bg: #232e3a;
        --td-input-fg: #e9edf3;
        --td-input-border: #3a4655;

        --td-select-bg: #232e3a;
        --td-select-panel: #232e3a;
        --td-select-border: #3a4655;
        --td-select-fg: #e9edf3;
        --td-select-ph: #97a2b1;
        --td-select-arrow: #a7b2c0;
        --td-select-sel: #2f3b4a;

        --td-btn-fg: #e9edf3;
        --td-btn-border: #5a6879;

        --td-overlay: #000000;
        --td-icon-filter: invert(1);
    }
}

/* Same values as the media query above - keep the two in sync. */
:root[data-theme="dark"] {
    color-scheme: dark;

    --td-bg: #161d26;
    --td-bg-raised: #1e2731;
    --td-surface: #1e2731;
    --td-fg: #e9edf3;
    --td-fg-base: #e9edf3;
    --td-fg-label: #a7b2c0;
    --td-rule: #2b3542;
    --td-mark: #3a4655;
    --td-link: #6fa3ff;

    --td-input-bg: #232e3a;
    --td-input-fg: #e9edf3;
    --td-input-border: #3a4655;

    --td-select-bg: #232e3a;
    --td-select-panel: #232e3a;
    --td-select-border: #3a4655;
    --td-select-fg: #e9edf3;
    --td-select-ph: #97a2b1;
    --td-select-arrow: #a7b2c0;
    --td-select-sel: #2f3b4a;

    --td-btn-fg: #e9edf3;
    --td-btn-border: #5a6879;

    --td-overlay: #000000;
    --td-icon-filter: invert(1);
}


/* ---- page ------------------------------------------------------------- */

html,
body {
    background-color: var(--td-bg);
    color: var(--td-fg);
}

/* <body> carries Fabric's class, and Content/fabric.css line 14282 declares
   `.ms-Fabric { color: #333333 }`. That is (0,1,0), so it beats a plain `body`
   rule no matter which stylesheet loads last - which is why the header nav and
   the footer note stayed dark grey on dark while everything with a rule of its
   own turned light. `body.ms-Fabric` (0,1,1) settles it without !important.
   Held at Fabric's own #333 in light mode so nothing shifts there. */
body.ms-Fabric {
    color: var(--td-fg-base);
}

.main__top {
    border-bottom-color: var(--td-rule);
}

/* Decorative block in the header's top-right corner. Left at #aeb9c7 in light
   mode; unchanged it reads as a bright smudge against the dark header. */
.main__top span {
    background-color: var(--td-mark);
}


/* ---- form ------------------------------------------------------------- */

/* Every field label is a bare <span> inside its .form__* wrapper. */
.form__function > span,
.form__symbol > span,
.form__interval > span,
.form__size > span,
.form__refresh > span,
.form__exchange > span,
.form__country > span,
.bottom__form-api > span {
    color: var(--td-fg-label);
}

/* The generated rows, addressed through their container rather than by class.
   BuildForm names each row .form__<parameter>, and style.css only ever had a
   rule for the five the pane was hand-written with - .form__symbol,
   .form__interval, .form__exchange, .form__size, .form__country. Every other
   parameter in the registry (mic_code, outputsize, isin, figi, cusip, ...)
   falls through both of the rules below: no bottom margin at all, so those
   rows packed tight against each other while symbol and interval had room,
   and no label colour, so they inherited --td-fg-base and came out
   near-white beside their muted grey neighbours on a dark theme.

   #td-fields makes these (1,0,1), which also flattens .form__symbol's and
   .form__exchange's legacy 20px to the 16px the rest of the sheet uses
   (.form__interval, .form__size, .form__refresh). That is deliberate: the
   form is entirely generated now, so which rows carry a legacy rule is an
   accident of the markup this replaced, not a rhythm worth preserving. The
   label is the row's direct child and the ⓘ is nested inside it, so the
   colour rule cannot reach the badge - span.td-help keeps its own. */
#td-fields > div {
    margin-bottom: 16px;
}

#td-fields > div > span {
    color: var(--td-fg-label);
}

/* The ⓘ inside a generated field's label, carrying openapi.json's own text in
   data-help. The rule lives here rather than in Home.css because that sheet
   loads before style.css and cannot see the palette variables.

   `span.td-help`, not `.td-help`: style.css's `.form__symbol span` (and the
   same rule for interval, exchange, country and size) is (0,1,1) and would
   otherwise make this glyph `display: block` too, dropping it onto its own
   line on exactly those rows. Matching that specificity and loading later
   settles it without !important.

   The badge is the label span's last child and the input is the span's next
   sibling, with no whitespace text node between them - so on the rows whose
   label stays inline (mic_code, and every other field style.css does not make
   `display: block`) the glyph sat flush against the input's border. The
   right margin is the larger of the two on purpose: the badge belongs to the
   label it annotates, not to the field beside it. It costs nothing on the
   block-label rows, where the input is already on its own line. */
span.td-help {
    display: inline;
    color: var(--td-fg-label);
    cursor: help;
    margin-left: 4px;
    margin-right: 8px;
    font-size: 12px;
}

/* The panel that ⓘ opens - see InitHelpTips in Home.js. This IS the tooltip:
   Office's task-pane webview does not render native `title` tooltips, so the
   attribute alone showed nothing on hover.

   Fixed, and a direct child of <body>, so no scrolling or overflow-hidden
   ancestor can clip it; JS positions it off the badge. max-width keeps it
   inside a pane that is typically ~320px wide, and max-height + scroll keeps
   the longest description (~900 characters) from running off the bottom. */
#td-tip {
    position: fixed;
    z-index: 1000;
    display: none;
    max-width: 260px;
    max-height: 40vh;
    overflow-y: auto;
    padding: 8px 10px;
    font-size: 12px;
    line-height: 1.4;
    background-color: var(--td-surface);
    color: var(--td-fg);
    border: 1px solid var(--td-select-border);
    border-radius: var(--td-radius);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

#td-tip.td-tip--on {
    display: block;
}

/* Native text inputs. Targeted by id so this beats style.css's
   `.form__size input` style rules without needing !important. */
#size,
#refresh,
#country,
#apikey {
    background-color: var(--td-input-bg);
    color: var(--td-input-fg);
    border-color: var(--td-input-border);
    border-radius: var(--td-radius);
}

#size::placeholder,
#refresh::placeholder,
#country::placeholder,
#apikey::placeholder {
    color: var(--td-fg-label);
    opacity: 0.7;
}

/* Get / Update. style.css sets a transparent background and a #111922 border
   but no colour, so the caption inherits body and vanishes on dark. */
.bottom__form-btn {
    color: var(--td-btn-fg);
    border-color: var(--td-btn-border);
    border-radius: var(--td-radius);
}

.bottom__form-wrapper {
    background-color: var(--td-bg-raised);
}

.inactive-bg {
    background: var(--td-overlay);
}


/* ---- select2 ---------------------------------------------------------- */
/* All four dropdowns (function, symbol, interval, exchange) are select2
   widgets, styled by the CDN stylesheet - the .jq-selectbox rules in
   style.css are dead code and do nothing here. */

/* The four corners of every dropdown.
   .select2-selection--single is rounded (4px), but style.css wraps it in
   `.select2-container { background: #f7f8fb !important }` - a SQUARE block of
   colour sitting behind a rounded child. What shows through the four corners is
   therefore that #f7f8fb, not the page. In light mode the box was #f7f8fb too,
   so it was invisible; on dark it reads as a bright notch at each corner.
   The container needs no background of its own - the selection paints one - so
   drop it. `!important` is required to beat theirs at equal specificity. */
.select2-container {
    background: transparent !important;
}

.select2-container--default .select2-selection--single {
    background-color: var(--td-select-bg);
    border-color: var(--td-select-border);
    border-radius: var(--td-radius);
}

/* select2's own default here is #444, not the body colour - hence a separate
   variable, so light mode stays byte-for-byte what it was. */
.select2-container--default .select2-selection--single .select2-selection__rendered {
    color: var(--td-select-fg);
}

.select2-container--default .select2-selection--single .select2-selection__placeholder {
    color: var(--td-select-ph);
}

.select2-container--default .select2-selection--single .select2-selection__arrow b {
    border-top-color: var(--td-select-arrow);
}

.select2-container--default.select2-container--open .select2-selection--single .select2-selection__arrow b {
    border-bottom-color: var(--td-select-arrow);
}

.select2-dropdown {
    background-color: var(--td-select-panel);
    border-color: var(--td-select-border);
    border-radius: var(--td-radius);
}

.select2-results__option {
    color: var(--td-input-fg);
}

/* The already-picked row. Left at select2's own #ddd in light mode; without an
   override it would stay #ddd on dark and swallow the light text. The
   --highlighted rule is deliberately not touched - select2 paints it
   #5897fb/white, which works on both palettes. */
.select2-container--default .select2-results__option--selected,
.select2-container--default .select2-results__option[aria-selected="true"] {
    background-color: var(--td-select-sel);
    color: var(--td-input-fg);
}

/* Search box inside the symbol-search dropdown. The .select2-container--default
   prefix is required: select2's own border rule carries it, and a bare
   `.select2-search--dropdown .select2-search__field` loses on specificity. */
.select2-container--default .select2-search--dropdown .select2-search__field {
    background-color: var(--td-select-panel);
    color: var(--td-select-fg);
    border-color: var(--td-select-border);
    border-radius: var(--td-radius);
}


/* ---- support popup and error panel ------------------------------------ */

.popup,
.error {
    background-color: var(--td-surface);
    color: var(--td-fg);
}

.popup-links,
.popup-links a,
.popup-bottom a,
.error-text a {
    color: var(--td-link);
}

/* magnificPopup's close glyph is hard-coded #333. */
.mfp-close-btn-in .mfp-close {
    color: var(--td-fg);
}

/* closebtn-icon.svg is a stroke="#111922" cross - invisible on dark. */
.error-closebtn {
    filter: var(--td-icon-filter);
}
