:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f1f1ef;
  --line: #e2e2de;
  --text: #191919;
  --text-2: #666666;
  --accent: #257de0;
  --accent-text: #ffffff;
  --gain: #0a7d1f;
  --loss: #c62f30;
  --warn-bg: #fff6e0;
  --warn-line: #f2c25b;
  --error-bg: #fdecec;
  --error-line: #ec9a9a;
  --info-bg: #eaf3fd;
  --info-line: #9dc8f7;
  --series-1: #257de0;
  /* Strategy colours: fixed order, validated palette; "Other" is neutral grey. */
  --s1: #257de0;
  --s2: #f47230;
  --s3: #00b894;
  --s4: #f2a600;
  --s5: #ee7fac;
  --s6: #008a17;
  --s7: #6a4ce2;
  --s8: #ec4647;
  --s-other: #8c8c8c;
  /* Running / stopped / check: status colours, always with an icon and a word */
  --run-bg: #dcf5e1; --run-text: #006c10; --run-line: #0aac23;
  --stop-bg: #ececec; --stop-text: #444444; --stop-line: #9a9a9a;
  --ask-bg: #fff3d1; --ask-text: #5c3f00; --ask-line: #fbb315;
  --btn-run-bg: #0a7d1f; --btn-run-text: #ffffff;
  --btn-stop-bg: #c62f30; --btn-stop-text: #ffffff;
  --grid: #ececec;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05);
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121212;
    --surface: #191919;
    --surface-2: #222222;
    --line: #2e2e2e;
    --text: #efefef;
    --text-2: #9c9c9c;
    --accent: #338ced;
    --gain: #3ccf55;
    --loss: #f07a7b;
    --warn-bg: #2c2410;
    --warn-line: #7a5d12;
    --error-bg: #2e1616;
    --error-line: #7c3131;
    --info-bg: #13233a;
    --info-line: #1b4e8a;
    --series-1: #338ced;
    --s1: #338ced;
    --s2: #e76624;
    --s3: #00a888;
    --s4: #d28e05;
    --s5: #e24d8b;
    --s6: #0aac23;
    --s7: #9887ef;
    --s8: #eb6263;
    --s-other: #7a7a7a;
    --run-bg: #12331b; --run-text: #6fdc7e; --run-line: #0aac23;
    --stop-bg: #2b2b2b; --stop-text: #c8c8c8; --stop-line: #6a6a6a;
    --ask-bg: #3a2c07; --ask-text: #ffd36b; --ask-line: #fbb315;
    --btn-run-bg: #0aac23; --btn-run-text: #0b0b0b;
    --btn-stop-bg: #dc3c3d; --btn-stop-text: #ffffff;
    --grid: #2a2a2a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; margin-top: 1.2em; }
.loading { padding: 2rem; color: var(--text-2); }
.muted { color: var(--text-2); }
.small { font-size: .85em; }
.strong { font-weight: 600; }
.gain { color: var(--gain); }
.loss { color: var(--loss); }

/* ---- Top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; margin-right: auto; }
.tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

main { max-width: 1240px; margin: 0 auto; padding: 16px; }
.foot { max-width: 1240px; margin: 0 auto; padding: 8px 16px 32px; color: var(--text-2); font-size: .8rem; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .6; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--loss); }
.btn.small { min-height: 28px; padding: 0 10px; font-size: .85em; }
.btn.big { min-height: 48px; padding: 0 28px; font-size: 1.05rem; }
.btn.icon { width: 36px; padding: 0; font-size: 1.2rem; }
.linkish { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; cursor: pointer; text-align: left; }
.linkish.strong { color: var(--text); font-weight: 600; }
.linkish.strong:hover { color: var(--accent); }
.linkish.gap { margin-left: 8px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); }
.segmented button { min-height: 34px; padding: 0 12px; border: 0; background: none; color: var(--text-2); font: inherit; font-weight: 600; cursor: pointer; }
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button.on { background: var(--accent); color: var(--accent-text); }
.segmented.small button { min-height: 28px; padding: 0 10px; font-size: .85em; }

/* ---- Notices, progress ---- */
.notices { display: grid; gap: 8px; margin-bottom: 12px; }
.notice { padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }
.notice.warn { background: var(--warn-bg); border-color: var(--warn-line); }
.notice.error { background: var(--error-bg); border-color: var(--error-line); }
.notice.info { background: var(--info-bg); border-color: var(--info-line); }
.notice .btn { margin-left: 6px; }
.status { margin: 0 0 12px; font-size: .85rem; }
.progress { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 0 0 12px; font-size: .9rem; }
.spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }
.tag { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 4px; background: var(--surface-2); color: var(--text-2); font-size: .75rem; font-weight: 500; }

/* ---- Landing ---- */
.landing { max-width: 640px; margin: 4vh auto; display: grid; gap: 16px; }
.landing h1 { font-size: clamp(1.5rem, 4vw, 2.1rem); }
.lead { font-size: 1.1rem; color: var(--text-2); margin: 0; }
.points { margin: 0; padding-left: 1.1em; display: grid; gap: 6px; }
.landing .btn.big { justify-self: start; }

/* ---- Cards ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.card .k { color: var(--text-2); font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.card .v { font-size: 1.45rem; font-weight: 700; margin: 2px 0; }
.card.hero .v { font-size: 1.7rem; }
.card .note { color: var(--text-2); font-size: .8rem; }
.cards.small { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.cards.small .v { font-size: 1.1rem; }

/* ---- Panels and tables ---- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; box-shadow: var(--shadow); }
.panelhead { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 4px; }
.panelhead h2 { margin: 0; }
.tablewrap { overflow-x: auto; margin: 8px -4px 0; }
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; white-space: nowrap; }
table.data th { font-size: .78rem; color: var(--text-2); font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
table.data .num { text-align: right; }
table.data td.note { white-space: normal; color: var(--text-2); font-size: .85em; min-width: 180px; }
table.data tbody tr:hover td { background: var(--surface-2); }
table.data.compact th, table.data.compact td { padding: 5px 6px; font-size: .88em; }
.sorter { background: none; border: 0; padding: 0; color: inherit; font: inherit; text-transform: inherit; letter-spacing: inherit; cursor: pointer; }
.namecell { display: inline-flex; flex-direction: column; gap: 1px; white-space: normal; }
.namecell .ext { text-decoration: none; font-size: .9em; }

/* ---- Chart ---- */
.chart { position: relative; min-height: 120px; margin-top: 6px; }
.chart svg { display: block; width: 100%; height: auto; touch-action: pan-y; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .zero { stroke: var(--text-2); stroke-width: 1; opacity: .5; }
.chart .axis { fill: var(--text-2); font-size: 11px; }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .cross { stroke: var(--text-2); stroke-width: 1; }
.chartlabel { font-size: .9rem; }
.chartlabel .v { font-weight: 700; font-size: 1.1rem; }
.tip { position: absolute; pointer-events: none; min-width: 150px; padding: 6px 10px; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); box-shadow: 0 2px 8px rgba(0, 0, 0, .12); font-size: .85rem; }
.tip strong { font-size: 1rem; }
/* Strategies day by day */
.sc1 { fill: var(--s1); background: var(--s1); }
.sc2 { fill: var(--s2); background: var(--s2); }
.sc3 { fill: var(--s3); background: var(--s3); }
.sc4 { fill: var(--s4); background: var(--s4); }
.sc5 { fill: var(--s5); background: var(--s5); }
.sc6 { fill: var(--s6); background: var(--s6); }
.sc7 { fill: var(--s7); background: var(--s7); }
.sc8 { fill: var(--s8); background: var(--s8); }
.scother { fill: var(--s-other); background: var(--s-other); }
.chart rect.seg { stroke: var(--surface); stroke-width: 1; transition: opacity .12s; }
.chart.focus rect.seg:not(.on) { opacity: .2; }
.chart .colhit { fill: transparent; }
.chart .colhit.hover { fill: var(--text); fill-opacity: .06; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 8px 0 2px; padding: 0; list-style: none; font-size: .85rem; }
.legend li { display: inline-flex; align-items: center; gap: 6px; cursor: default; }
.legend li.dim { opacity: .4; }
.legend .sw { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.legend .amt { color: var(--text-2); }
.legend .amt.gain, .tip .amt.gain { color: var(--gain); }
.legend .amt.loss, .tip .amt.loss { color: var(--loss); }
.tip .row { display: flex; align-items: center; gap: 6px; }
.tip .row .amt { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }
.tip .sw { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.chartsum { display: flex; flex-wrap: wrap; gap: 2px 16px; font-size: .9rem; }
.empty { padding: 24px 0; text-align: center; }

/* ---- Dialogs ---- */
dialog.sheet { width: min(760px, 100vw - 24px); max-height: calc(100vh - 24px); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px 20px; background: var(--surface); color: var(--text); overflow: auto; }
dialog.sheet.narrow { width: min(460px, 100vw - 24px); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.sheethead { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.sheethead h2 { margin: 0; }
.form { display: grid; gap: 10px; margin: 12px 0; }
.form label { display: grid; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--text-2); }
.form input, .form select { min-height: 38px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-weight: 400; }
.formrow { display: flex; gap: 8px; flex-wrap: wrap; }
.fielderror { color: var(--loss); font-size: .85rem; margin: 4px 0 0; }
.fielderror:empty { display: none; }
label.check { display: flex; gap: 8px; align-items: center; margin: 12px 0; }

/* ---- Phones: tables become cards ---- */
@media (max-width: 760px) {
  main { padding: 12px; }
  .topbar { gap: 8px; }
  .tools { width: 100%; justify-content: space-between; }
  .cards { grid-template-columns: 1fr 1fr; }
  .card.hero { grid-column: 1 / -1; }
  .card .v { font-size: 1.2rem; }
  .panel { padding: 12px; }
  .tablewrap { overflow: visible; margin: 8px 0 0; }
  table.data thead { display: none; }
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data tr { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  table.data td { border: 0; padding: 2px 0; white-space: normal; text-align: left !important; }
  table.data td:first-child { grid-column: 1 / -1; padding-bottom: 6px; }
  table.data td:not(:first-child)::before { content: attr(data-label); display: block; color: var(--text-2); font-size: .72rem; text-transform: uppercase; letter-spacing: .02em; }
  table.data tbody tr:hover td { background: none; }
  table.data td.note { grid-column: 1 / -1; }
  dialog.sheet { width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: 14px 14px 0 0; }
}

/* ---- Running / stopped ---- */
.pill { display: inline-flex; align-items: center; gap: 2px; padding: 2px 10px; border-radius: 999px; border: 1px solid transparent; font-size: .78rem; font-weight: 700; letter-spacing: .02em; white-space: nowrap; }
.pill.big { font-size: .9rem; padding: 4px 12px; }
.pill.tiny { font-size: .7rem; padding: 0 7px; margin-left: 4px; }
.pill.run { background: var(--run-bg); color: var(--run-text); border-color: var(--run-line); }
.pill.stop { background: var(--stop-bg); color: var(--stop-text); border-color: var(--stop-line); }
.pill.ask { background: var(--ask-bg); color: var(--ask-text); border-color: var(--ask-line); }
.statuscell { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.statusline { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 4px 0 12px; }
.askbtns { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.btn.stopbtn { background: var(--btn-stop-bg); border-color: var(--btn-stop-bg); color: var(--btn-stop-text); font-weight: 700; }
.btn.runbtn { background: var(--btn-run-bg); border-color: var(--btn-run-bg); color: var(--btn-run-text); font-weight: 700; }
.btn.stopbtn:hover, .btn.runbtn:hover { filter: brightness(1.08); }

/* ---- Strategies chart period ---- */
.rangebar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.rangebar label { display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; color: var(--text-2); }
.dateinput { min-height: 30px; padding: 2px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; font-size: .85rem; }

/* ---- Row menu (⋯) ---- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
table.data th.menucol, table.data td.menucol { width: 44px; text-align: right; }
.btn.menubtn { position: relative; min-width: 34px; min-height: 30px; font-size: 1.1rem; line-height: 1; }
.btn.menubtn.attention::after { content: ''; position: absolute; top: 3px; right: 3px; width: 8px; height: 8px; border-radius: 50%; background: var(--ask-line); box-shadow: 0 0 0 2px var(--surface); }
.rowmenu { position: absolute; z-index: 50; min-width: 200px; padding: 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); box-shadow: 0 6px 24px rgba(0, 0, 0, .18); }
.menuitem { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%; min-height: 38px; padding: 0 10px; border: 0; border-radius: 6px; background: none; color: var(--text); font: inherit; font-weight: 600; text-align: left; cursor: pointer; }
.menuitem:hover, .menuitem:focus-visible { background: var(--surface-2); }
.menuitem.stopitem { color: var(--btn-stop-bg); }
.menuitem.runitem { color: var(--run-text); }
.menuitem .check { font-weight: 700; }
.menusep { height: 1px; margin: 4px 2px; background: var(--line); }
.menunote { padding: 4px 10px 6px; font-size: .8rem; color: var(--ask-text); background: var(--ask-bg); border-radius: 6px; margin-bottom: 4px; }
@media (max-width: 760px) {
  table.data tr { position: relative; }
  table.data td.menucol { position: absolute; top: 8px; right: 0; width: auto; padding: 0; }
  table.data td.menucol::before { content: none; }
  table.data td:first-child { padding-right: 48px; }
  .rangebar { width: 100%; }
}

/* ---- Balance line under the strategies chart ---- */
.chart .balline { fill: none; stroke: var(--text); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .baldot { fill: var(--text); stroke: var(--surface); stroke-width: 2; }
.chart .baldot.ring { fill: var(--surface); stroke: var(--text); }
.chart .paneltitle { fill: var(--text-2); font-size: 12px; font-weight: 600; }
.swline { display: inline-block; width: 14px; height: 0; border-top: 2px solid var(--text); vertical-align: middle; flex: none; }
.tip .balrow { margin-top: 4px; padding-top: 4px; border-top: 1px solid var(--line); }
.chart .depline { fill: none; stroke: var(--text-2); stroke-width: 2; stroke-dasharray: 6 4; }
.chart .gainfill { fill: var(--gain); fill-opacity: .22; stroke: none; }
.chart .lossfill { fill: var(--loss); fill-opacity: .22; stroke: none; }
.chart .depmark { stroke: var(--surface); stroke-width: 1.5; }
.chart .depmark.in { fill: var(--gain); }
.chart .depmark.out { fill: var(--loss); }
.chart .deplabel { fill: var(--text); font-size: 11px; font-weight: 600; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; }
.swline.dash { border-top: 2px dashed var(--text-2); }
.tip .balrow .row { gap: 6px; }

/* ---- Accounts total row ---- */
table.data tfoot td { border-top: 2px solid var(--text-2); border-bottom: 0; font-weight: 700; background: var(--surface-2); }
@media (max-width: 760px) {
  table.data tfoot { display: block; width: 100%; }
  table.data tfoot tr { background: var(--surface-2); border-radius: 8px; padding: 10px; border-bottom: 0; margin-top: 8px; }
  table.data tfoot td { border-top: 0; background: none; }
}

/* ---- Widgets: winners, losers, working days ---- */
.widgets { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
.widgets .panel { margin-bottom: 0; }
.widget h2 { margin: 0 0 2px; }
.piebox { display: flex; justify-content: center; margin: 8px 0; }
.piesvg { width: 100%; max-width: 210px; height: auto; display: block; }
.piesvg .slice { stroke: var(--surface); stroke-width: 2; transition: opacity .12s; }
.piebox.focus .slice:not(.on) { opacity: .2; }
.piesvg .pietotal { font-size: 18px; font-weight: 700; }
.piesvg .pietotal.gain { fill: var(--gain); }
.piesvg .pietotal.loss { fill: var(--loss); }
.piesvg .piesub { fill: var(--text-2); font-size: 11px; }
.pielegend { flex-direction: column; flex-wrap: nowrap; gap: 4px; margin: 0; }
.pielegend li { display: grid; grid-template-columns: 10px minmax(0, 1fr) auto 40px; gap: 8px; align-items: center; }
.pielegend .pn { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pielegend .amt { font-variant-numeric: tabular-nums; text-align: right; }
.pielegend .amt.gain { color: var(--gain); }
.pielegend .amt.loss { color: var(--loss); }
.pielegend .pct { color: var(--text-2); text-align: right; font-variant-numeric: tabular-nums; }
.wdbox { margin: 8px 0 4px; }
.wdsvg { width: 100%; height: auto; display: block; }
.wdsvg .zero { stroke: var(--text-2); stroke-width: 1; opacity: .6; }
.wdsvg .wdbar.gain { fill: var(--gain); }
.wdsvg .wdbar.loss { fill: var(--loss); }
.wdsvg .wdval { fill: var(--text); font-size: 12px; font-weight: 600; }
.wdsvg .axis { fill: var(--text-2); font-size: 12px; }
@media (max-width: 1000px) { .widgets { grid-template-columns: 1fr; } }
.piesvg .slice.whole { stroke: none; }
