/* Strategy optimizer (app/strategy-optimizer.js). Builds on the dialog styles in app/alerts.css. */

.so-dialog { width: min(760px, 100%); }
.so-title { display: flex; flex-direction: column; gap: 2px; }
.so-kicker { color: var(--text-muted); font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; }
.so-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.so-body { display: flex; flex-direction: column; gap: 14px; }
.so-label { color: var(--text-muted); font-size: 13px; }
.so-muted { color: var(--text-muted); }
.so-body .up { color: var(--up); }
.so-body .down { color: var(--down); }
.mono { font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 13px; }

/* ---- setup */
.so-setup { display: flex; flex-direction: column; gap: 12px; }
.so-setup[hidden], .so-results[hidden], .so-progress[hidden] { display: none; }
.so-goal { display: flex; align-items: center; gap: 12px; }
.so-opts { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 24px; }
.so-limit-input { width: 110px; height: 38px; }
/* Mode: the page's own dropdown; its menu opens just under the button, as wide as it */
.so-dd { position: relative; }
.so-dd-btn { display: inline-flex; align-items: center; justify-content: space-between; gap: 10px; min-width: 150px; height: 38px; cursor: pointer; text-align: left; }
.so-dd-btn:disabled { cursor: default; }
.so-dd.is-open .so-dd-btn { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.so-dd-chev { display: grid; color: var(--text-muted); transition: transform .15s ease; }
.so-dd-chev svg { width: 16px; height: 16px; }
.so-dd.is-open .so-dd-chev { transform: rotate(180deg); }
.so-dd-menu { position: absolute; top: calc(100% + 6px); left: 0; right: 0; z-index: 20; display: flex; flex-direction: column; gap: 2px;
    padding: 5px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); box-shadow: 0 16px 40px rgba(0, 0, 0, .4); }
.so-dd-menu[hidden] { display: none; }
.so-dd-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 7px; font-size: 14px; color: var(--text); text-align: left; }
.so-dd-item:hover, .so-dd-item:focus-visible { background: var(--surface-2); outline: none; }
.so-dd-check { width: 14px; color: var(--accent); font-weight: 700; visibility: hidden; }
.so-dd-item.is-on { font-weight: 600; }
.so-dd-item.is-on .so-dd-check { visibility: visible; }
/* Never taller than the screen: the results scroll inside, the buttons stay in view. */
.so-dialog { max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; }
.so-dialog .so-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.so-dialog .al-dhead, .so-dialog .so-foot { flex: none; }
@media (min-width: 761px) { .so-opts { flex-wrap: nowrap; gap: 10px 18px; } .so-opts .so-goal { flex: none; } }
/* Automatic mode: the grid and Max combinations are greyed and locked; Optimize for stays. */
.so-dialog.is-auto .so-grid, .so-dialog.is-auto .so-grid-title, .so-dialog.is-auto .so-limit { opacity: .42; pointer-events: none; }
.so-dialog.is-auto .so-grid { filter: grayscale(1); }
/* The automatic search's status, above its live list. */
.so-live { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 14px; padding: 4px 0 14px; }
.so-live.is-stopped .so-spin { visibility: hidden; width: 0; }
.so-live-list:empty { display: none; }
.so-select { min-width: 200px; height: 38px; cursor: pointer; }
.so-grid-title { margin-top: 4px; }
.so-grid { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.so-row {
    display: grid;
    grid-template-columns: 22px minmax(120px, 1.4fr) repeat(3, minmax(64px, 1fr)) 72px;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-top: 1px solid var(--border);
}
.so-row:first-child { border-top: 0; }
.so-row--head { padding-top: 7px; padding-bottom: 7px; background: var(--surface-2); color: var(--text-muted); font-size: 12px; }
/* Checkboxes drawn in the theme (the browser's unticked box is white in dark mode). */
.so-dialog input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    display: inline-grid;
    place-items: center;
    flex: none;
    width: 16px;
    height: 16px;
    margin: 0;
    border: 1.5px solid var(--text-muted);
    border-radius: 4px;
    background: var(--surface);
    cursor: pointer;
    transition: background .12s, border-color .12s;
}
.so-dialog input[type="checkbox"]:hover { border-color: var(--text); }
.so-dialog input[type="checkbox"]:checked { border-color: var(--accent); background: var(--accent); }
.so-dialog input[type="checkbox"]:checked::after {
    content: "";
    width: 4px;
    height: 8px;
    margin-top: -2px;
    border: solid #fff;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}
.so-dialog input[type="checkbox"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.so-dialog input[type="checkbox"]:disabled { opacity: .4; cursor: default; }
.so-row.is-off .so-input-name, .so-row.is-off .so-options:not(.has-fixed) { opacity: .5; }
.so-fixed { width: auto; min-width: 160px; max-width: 100%; height: 34px; }
.so-input-name { display: flex; flex-direction: column; min-width: 0; font-weight: 500; }
.so-input-name small { color: var(--text-muted); font-size: 12px; font-weight: 400; }
.so-options { grid-column: span 3; color: var(--text-muted); font-size: 13px; }
.so-field { display: contents; }
.so-field-cap { display: none; }
.so-num { width: 100%; height: 34px; min-width: 0; padding: 0 8px; box-sizing: border-box; font-size: 14px; }
.so-num:disabled { opacity: .45; }
/* No browser spinner arrows: values are typed (the arrow keys still step them). */
.so-num { -moz-appearance: textfield; appearance: textfield; font-variant-numeric: tabular-nums; }
.so-num::-webkit-inner-spin-button, .so-num::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.so-count { color: var(--text-muted); font-size: 12px; text-align: right; }
.so-summary { margin: 0; color: var(--text-muted); font-size: 13px; }
.so-summary.is-error { color: var(--down); }
.so-note { margin: 0; color: var(--text-muted); font-size: 12px; }

/* ---- loading */
.so-progress { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 14px 14px; padding: 6px 0; }
.so-spin { position: relative; width: 38px; height: 38px; }
.so-spin::before, .so-spin span {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 3px solid var(--surface-2);
}
.so-spin span { border-color: var(--accent) transparent transparent transparent; animation: so-spin .8s linear infinite; }
.so-progress-text { display: flex; flex-direction: column; gap: 2px; }
.so-progress-text strong { font-size: 15px; }
.so-progress-sub { color: var(--text-muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.so-bar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.so-bar span {
    display: block;
    height: 100%;
    width: 0;
    border-radius: 3px;
    background: linear-gradient(90deg, var(--accent), var(--up));
    transition: width .25s ease;
}
.so-skeleton { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 8px; }
.so-skeleton i {
    display: block;
    height: 30px;
    border-radius: 6px;
    background: linear-gradient(90deg, var(--surface-2) 0%, var(--border) 50%, var(--surface-2) 100%);
    background-size: 200% 100%;
    animation: so-shimmer 1.3s ease-in-out infinite;
}
.so-skeleton i:nth-child(2) { width: 88%; animation-delay: .1s; }
.so-skeleton i:nth-child(3) { width: 94%; animation-delay: .2s; }
.so-skeleton i:nth-child(4) { width: 80%; animation-delay: .3s; }
@keyframes so-spin { to { transform: rotate(360deg); } }
@keyframes so-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }
@media (prefers-reduced-motion: reduce) {
    .so-spin span, .so-skeleton i { animation-duration: 3s; }
}

/* ---- results */
.so-results { display: flex; flex-direction: column; gap: 10px; }
.so-best { display: flex; flex-direction: column; gap: 12px; padding: 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); }
.so-best-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 12px; }
.so-best-head strong { font-size: 16px; }
.so-best-head .so-muted { font-size: 12px; }
.so-kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.so-kpi { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.so-kpi > .so-muted { font-size: 12px; }
.so-kpi strong { font: 600 18px/1.2 "JetBrains Mono", ui-monospace, monospace; }
.so-kpi small { font-size: 12px; }
.so-chips { display: flex; flex-wrap: wrap; gap: 6px; }
/* The best settings: a list with its own scroll bar */
.so-vals { border: 1px solid var(--border); border-radius: 10px; background: var(--surface); overflow: hidden; }
.so-vals-row { display: grid; grid-template-columns: minmax(0, 1fr) 150px 150px; align-items: center; gap: 12px; padding: 7px 14px; font-size: 13.5px; }
.so-vals-head { padding: 8px 14px; font-size: 12px; font-weight: 600; letter-spacing: .02em; color: var(--text-muted); background: var(--surface-2); border-bottom: 1px solid var(--border); }
.so-vals-head span:nth-child(n+2), .so-vals-best, .so-vals-now { text-align: right; }
.so-vals-body { max-height: 196px; overflow-y: auto; scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
.so-vals-body::-webkit-scrollbar { width: 8px; }
.so-vals-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; border: 2px solid var(--surface); }
.so-vals-body::-webkit-scrollbar-track { background: transparent; }
.so-vals-body .so-vals-row + .so-vals-row { border-top: 1px solid var(--border); }
.so-vals-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-muted); }
.so-vals-best { font-weight: 600; }
.so-vals-best, .so-vals-now { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.so-vals-now { color: var(--text-muted); }
.so-vals-row.is-changed { background: var(--accent-soft); }
.so-vals-row.is-changed .so-vals-name { color: var(--text); }
.so-vals-row.is-changed .so-vals-best { color: var(--accent); }
@media (max-width: 560px) { .so-vals-row { grid-template-columns: minmax(0, 1fr) 80px 70px; gap: 8px; padding: 7px 10px; } }
.so-chip { padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); font-size: 13px; }
.so-chip.is-changed { border-color: var(--accent); background: var(--accent-soft); }
.so-apply-best { align-self: flex-start; }
.so-table-title { margin-top: 4px; }
.so-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
.so-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.so-table th, .so-table td { padding: 7px 10px; border-top: 1px solid var(--border); text-align: left; white-space: nowrap; }
.so-table th { border-top: 0; background: var(--surface-2); color: var(--text-muted); font-weight: 500; font-size: 12px; }
.so-table .num { text-align: right; }
.so-table tr.is-current td { background: var(--accent-soft); }
.so-table .so-apply { height: 28px; padding: 0 12px; font-size: 13px; }
.so-tag { display: inline-block; padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-muted); font-size: 12px; }

/* ---- footer */
.so-foot { flex-wrap: wrap; }
.so-auto { display: inline-flex; align-items: center; gap: 6px; color: var(--text-muted); font-size: 13px; cursor: pointer; }
.so-foot .al-btn[hidden] { display: none; }

/* ---- Strategy panel button */
.st-optimize {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 30px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: none;
    color: var(--text);
    font: 500 13px/1 Inter, -apple-system, "Segoe UI", sans-serif;
    cursor: pointer;
}
.st-optimize[hidden] { display: none; }
.st-optimize svg { width: 15px; height: 15px; color: var(--accent); }
.st-optimize:hover { background: var(--surface-2); }
/* Premium only: a crown while locked. */
.st-optimize .st-lock { display: none; width: 13px; height: 13px; color: #a78bfa; }
.st-optimize.is-locked .st-lock { display: block; }

@media (max-width: 640px) {
    /* The dialog fits the screen: title and buttons stay put, only the middle scrolls. */
    .al-back.so-back { padding: 8px; align-items: flex-end; }
    .so-dialog { max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); margin: 0; }
    .so-dialog .al-dhead { padding: 10px 10px 10px 16px; }
    .so-dialog .al-dtitle { font-size: 16px; }
    .so-kicker { font-size: 11px; }
    .so-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 14px 16px; gap: 12px; }
    .so-goal { gap: 10px; }
    .so-goal .so-label { flex: none; }
    .so-select { flex: 1; min-width: 0; }
    .so-opts .so-goal { flex: 1 0 100%; }
    .so-limit-input { flex: 1; width: auto; min-width: 0; }
    /* Each input on two lines: name and value count, then From / To / Step. */
    .so-row { grid-template-columns: 22px 1fr 1fr 1fr; gap: 6px 8px; padding: 10px 12px; }
    .so-row--head { display: none; }
    .so-row .so-input-name { grid-column: 2 / 4; grid-row: 1; }
    .so-count { grid-column: 4; grid-row: 1; }
    .so-options { grid-column: 2 / -1; grid-row: 2; }
    .so-field { display: flex; flex-direction: column; gap: 3px; min-width: 0; grid-row: 2; }
    .so-field:nth-child(3) { grid-column: 2; }
    .so-field:nth-child(4) { grid-column: 3; }
    .so-field:nth-child(5) { grid-column: 4; }
    .so-field-cap { display: block; color: var(--text-muted); font-size: 11px; }
    /* 16px stops iPhones zooming the page when a box is tapped. */
    .so-num, .so-select { font-size: 16px; }
    .so-foot { gap: 8px 10px; padding: 10px 16px; }
    .so-auto { flex: 1 0 100%; }
    .so-foot .qd-spacer { display: none; }
    .so-foot .al-btn { flex: 1; padding: 0 10px; }
    .so-kpis { grid-template-columns: repeat(2, 1fr); }
    .st-optimize span { display: none; }
}
