:root,
:root[data-theme="modern-financial"] {
  color-scheme: light;
  --text-primary: #173029;
  --text-muted: #60736b;
  --border-default: #d4dfda;
  --surface-page: #eaf3ef;
  --surface-panel: #ffffff;
  --surface-group: #fbfdfc;
  --surface-workspace: #f8fcfa;
  --surface-soft: #f2f7f5;
  --surface-input: #ffffff;
  --header-bg: #123c32;
  --header-text: #ffffff;
  --header-muted: #d4e5df;
  --header-accent: #c7a24d;
  --header-control: rgba(255, 255, 255, .1);
  --header-control-border: rgba(255, 255, 255, .28);
  --action-primary: #087b68;
  --action-primary-hover: #066454;
  --action-text: #ffffff;
  --action-secondary: #e8f0ed;
  --action-secondary-hover: #dbe9e4;
  --link: #176d91;
  --positive: #167748;
  --warning: #a5600a;
  --negative: #b33434;
  --info-bg: #edf6fb;
  --info-text: #254455;
  --error-bg: #fff3f3;
  --error-text: #713333;
  --focus-ring: rgba(8, 123, 104, .22);
  --shadow-panel: 0 14px 35px rgba(24, 50, 42, .08);
  --shadow-group: 0 3px 12px rgba(23, 48, 40, .04);
  --shadow-control: 0 1px 3px rgba(23, 33, 28, .22);
}

:root[data-theme="professional-neutral"] {
  --text-primary: #202832;
  --text-muted: #65707d;
  --border-default: #d6dce3;
  --surface-page: #edf2f6;
  --surface-panel: #ffffff;
  --surface-group: #fafbfc;
  --surface-workspace: #f7f9fb;
  --surface-soft: #f3f5f7;
  --surface-input: #ffffff;
  --header-bg: #293541;
  --header-muted: #d9e0e6;
  --header-accent: #6da4ce;
  --header-control: rgba(255, 255, 255, .08);
  --action-primary: #256fa8;
  --action-primary-hover: #1d5a89;
  --action-secondary: #e9edf1;
  --action-secondary-hover: #dde3e9;
  --link: #256fa8;
  --positive: #2c7a50;
  --warning: #9a6716;
  --negative: #b03a43;
  --info-bg: #edf4fa;
  --info-text: #314f67;
  --focus-ring: rgba(37, 111, 168, .22);
  --shadow-panel: 0 12px 30px rgba(32, 40, 50, .09);
  --shadow-group: 0 3px 12px rgba(32, 40, 50, .05);
}

:root[data-theme="market-terminal"] {
  --text-primary: #182520;
  --text-muted: #5d6c65;
  --border-default: #cfd9d4;
  --surface-page: #e5eee9;
  --surface-panel: #fbfdfc;
  --surface-group: #f7faf8;
  --surface-workspace: #f2f7f4;
  --surface-soft: #eaf1ed;
  --surface-input: #ffffff;
  --header-bg: #202d29;
  --header-muted: #d3ddd8;
  --header-accent: #39a478;
  --header-control: rgba(255, 255, 255, .08);
  --action-primary: #246c50;
  --action-primary-hover: #19583e;
  --action-secondary: #dfe9e4;
  --action-secondary-hover: #d1dfd8;
  --link: #176d75;
  --positive: #087d45;
  --warning: #a06a08;
  --negative: #bd3038;
  --info-bg: #e6f1ef;
  --info-text: #244a43;
  --focus-ring: rgba(36, 108, 80, .24);
  --shadow-panel: 0 8px 22px rgba(24, 37, 32, .08);
  --shadow-group: 0 2px 8px rgba(24, 37, 32, .05);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --text-primary: #e7eeeb;
  --text-muted: #a9b8b2;
  --border-default: #42524c;
  --surface-page: #111816;
  --surface-panel: #1c2622;
  --surface-group: #19231f;
  --surface-workspace: #17201d;
  --surface-soft: #26332e;
  --surface-input: #121a17;
  --header-bg: #0b100e;
  --header-text: #f4f8f6;
  --header-muted: #b8c7c1;
  --header-accent: #68bfa0;
  --header-control: #18231f;
  --header-control-border: #53675f;
  --action-primary: #299576;
  --action-primary-hover: #36aa89;
  --action-text: #ffffff;
  --action-secondary: #2a3732;
  --action-secondary-hover: #35463f;
  --link: #77bfdd;
  --positive: #319d69;
  --warning: #c88328;
  --negative: #d55d63;
  --info-bg: #1b302f;
  --info-text: #c7deda;
  --error-bg: #3a2224;
  --error-text: #f0c4c7;
  --focus-ring: rgba(76, 190, 153, .35);
  --shadow-panel: 0 14px 36px rgba(0, 0, 0, .3);
  --shadow-group: 0 3px 12px rgba(0, 0, 0, .2);
  --shadow-control: 0 1px 4px rgba(0, 0, 0, .55);
}

:root[data-theme="high-contrast"] {
  --text-primary: #000000;
  --text-muted: #313131;
  --border-default: #525252;
  --surface-page: #ffffff;
  --surface-panel: #ffffff;
  --surface-group: #ffffff;
  --surface-workspace: #ffffff;
  --surface-soft: #f0f0f0;
  --surface-input: #ffffff;
  --header-bg: #000000;
  --header-muted: #ffffff;
  --header-accent: #ffd400;
  --header-control: #111111;
  --header-control-border: #ffffff;
  --action-primary: #004f9f;
  --action-primary-hover: #003b78;
  --action-secondary: #ffffff;
  --action-secondary-hover: #e8e8e8;
  --link: #004f9f;
  --positive: #006b35;
  --warning: #865100;
  --negative: #a40000;
  --info-bg: #e9f4ff;
  --info-text: #000000;
  --error-bg: #fff0f0;
  --error-text: #710000;
  --focus-ring: rgba(0, 79, 159, .35);
  --shadow-panel: 0 0 0 1px #525252;
  --shadow-group: none;
  --shadow-control: 0 0 0 1px #000000;
}

:root[data-theme="market-terminal"] section:not(.portfolio-editor) { padding: 16px; }
:root[data-theme="market-terminal"] .card { padding: 12px; }
:root[data-theme="market-terminal"] input,
:root[data-theme="market-terminal"] select,
:root[data-theme="market-terminal"] button { min-height: 39px; }
:root[data-theme="market-terminal"] .metric { min-height: 76px; }

:root[data-theme="high-contrast"] section,
:root[data-theme="high-contrast"] .card,
:root[data-theme="high-contrast"] .form-group,
:root[data-theme="high-contrast"] input,
:root[data-theme="high-contrast"] select,
:root[data-theme="high-contrast"] textarea {
  border-width: 2px;
}

:root[data-theme="high-contrast"] button { font-weight: 900; }

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text-primary);
  background: var(--surface-page);
}

header {
  background: var(--header-bg);
  color: var(--header-text);
  padding: 24px clamp(16px, 4vw, 44px);
  border-bottom: 4px solid var(--header-accent);
}

.header-inner {
  max-width: 1380px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
}

.header-tools {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.theme-control {
  margin: 0;
  color: var(--header-muted);
}

.theme-control span {
  display: block;
  margin-bottom: 4px;
  font-size: 11px;
  font-weight: 800;
}

.theme-control select {
  min-width: 180px;
  min-height: 36px;
  border-color: var(--header-control-border);
  background: var(--header-control);
  color: var(--header-text);
  font-size: 13px;
}

.theme-control option {
  background: var(--surface-panel);
  color: var(--text-primary);
}

h1, h2, h3 { margin: 0; letter-spacing: 0; }
h1 { font-size: 30px; line-height: 1.1; }
h2 { font-size: 18px; margin-bottom: 14px; }
h3 { font-size: 15px; margin: 14px 0 8px; }
p { line-height: 1.5; }

.subtitle {
  margin: 8px 0 0;
  color: var(--header-muted);
  font-size: 15px;
}

.status-pill {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--header-control-border);
  border-radius: 8px;
  padding: 8px 10px;
  white-space: nowrap;
  color: var(--header-text);
  font-size: 13px;
  font-weight: 750;
}

main {
  max-width: 1380px;
  margin: 0 auto;
  padding: 24px clamp(16px, 4vw, 44px) 44px;
}

.grid {
  display: grid;
  grid-template-columns: minmax(420px, 520px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

section, .card {
  background: var(--surface-panel);
  border: 1px solid var(--border-default);
  border-radius: 8px;
  box-shadow: var(--shadow-panel);
}

section { min-width: 0; padding: 18px; }
.card { padding: 14px; box-shadow: none; }

.portfolio-editor {
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.portfolio-editor > h2 {
  margin: 2px 2px 12px;
}

.form-group {
  margin-bottom: 14px;
  padding: 14px;
  border: 1px solid var(--border-default);
  border-radius: 8px;
  background: var(--surface-group);
  box-shadow: var(--shadow-group);
}

.form-group > h3 {
  margin: 0 0 4px;
}

.analysis-workspace {
  background: var(--surface-workspace);
}

.analysis-workspace .card {
  background: var(--surface-panel);
}

label {
  display: block;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 800;
  margin: 12px 0 6px;
}

input, select, textarea {
  width: 100%;
  min-width: 0;
  min-height: 42px;
  border: 1px solid var(--border-default);
  border-radius: 7px;
  padding: 9px 10px;
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-input);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

textarea {
  min-height: 180px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 13px;
}

button {
  min-height: 42px;
  border: 0;
  border-radius: 7px;
  padding: 9px 12px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  background: var(--action-primary);
  color: var(--action-text);
}

button:hover { background: var(--action-primary-hover); }

button.secondary {
  background: var(--action-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
}

button.secondary:hover { background: var(--action-secondary-hover); }

button:disabled {
  cursor: not-allowed;
  opacity: .52;
}

body.request-active button:disabled {
  cursor: wait;
}

.compatibility-message {
  margin: 9px 0 0;
  color: var(--warning);
  font-size: 12px;
  line-height: 1.45;
}

.row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.row > div,
.holding-field,
.period-field,
.holding-period-control {
  min-width: 0;
}

.horizon-control {
  display: grid;
  grid-template-columns: .7fr 1fr;
  gap: 8px;
}

.holding-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(112px, 1.15fr) minmax(64px, .55fr) minmax(86px, .72fr) minmax(136px, .95fr);
  gap: 8px;
  align-items: end;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid var(--border-default);
  border-radius: 8px;
  background: var(--surface-soft);
}

.holding-row label { margin-top: 0; }

.holding-period-control {
  display: grid;
  grid-template-columns: minmax(54px, .62fr) minmax(82px, 1fr);
  gap: 6px;
}

.icon-button {
  position: absolute;
  top: -7px;
  right: -7px;
  display: inline-flex;
  width: 40px;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--action-text);
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
}

.icon-button::before {
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--action-primary);
  box-shadow: var(--shadow-control);
  content: "";
}

.icon-button span {
  position: relative;
  z-index: 1;
  transform: translateY(-1px);
}

.icon-button:hover,
.icon-button:focus-visible {
  background: transparent;
  color: var(--action-text);
}

.icon-button:hover::before,
.icon-button:focus-visible::before {
  background: var(--negative);
}

.actions, .tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.tab {
  background: var(--action-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
}

.tab:hover { background: var(--action-secondary-hover); }
.tab.active, .tab.active:hover { background: var(--action-primary); color: var(--action-text); }
.panel { display: none; }
.panel.active { display: block; }

.notice {
  border-left: 4px solid var(--link);
  background: var(--info-bg);
  padding: 10px 12px;
  border-radius: 6px;
  color: var(--info-text);
  font-size: 13px;
  margin-top: 14px;
}

.error-notice {
  border-left-color: var(--negative);
  background: var(--error-bg);
  color: var(--error-text);
}

.trace {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 7px;
  background: var(--surface-soft);
  border: 1px solid var(--border-default);
  color: var(--text-muted);
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  margin: 0 0 12px;
}

.summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric {
  border: 1px solid var(--border-default);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 12px;
  min-height: 82px;
}

.metric span { color: var(--text-muted); font-size: 12px; font-weight: 800; }
.metric strong { display: block; margin-top: 8px; font-size: 18px; }

.cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 10px;
}

.symbol { font-size: 18px; font-weight: 850; }
.muted { color: var(--text-muted); font-size: 13px; }

.source-link {
  color: var(--link);
  font-weight: 800;
  text-decoration: none;
}

.source-link:hover { text-decoration: underline; }

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 5px 9px;
  color: var(--action-text);
  font-size: 12px;
  font-weight: 850;
  white-space: nowrap;
  background: var(--link);
}

.Keep { background: var(--positive); }
.Review, .Insufficient { background: var(--warning); }
.Reduce, .Exit-watch { background: var(--negative); }

.fact-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.fact {
  background: var(--surface-soft);
  border-radius: 7px;
  padding: 8px;
  font-size: 12px;
  min-height: 58px;
}

.fact b { display: block; font-size: 14px; margin-top: 4px; }
.context-box {
  border: 1px solid var(--border-default);
  border-radius: 7px;
  background: var(--surface-soft);
  padding: 10px;
  font-size: 13px;
}
ul { margin: 8px 0 0 18px; padding: 0; }
li { margin-bottom: 6px; }
pre { overflow: auto; background: var(--surface-soft); border: 1px solid var(--border-default); border-radius: 8px; padding: 12px; }

@media (max-width: 940px) {
  .grid, .cards, .summary { grid-template-columns: 1fr; }
  .header-inner { flex-direction: column; }
  .header-tools { width: 100%; justify-content: space-between; }
}

@media (max-width: 700px) {
  .holding-row {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .symbol-field { grid-column: span 3; }
  .qty-field { grid-column: span 3; }
  .avg-field { grid-column: span 3; }
  .period-field { grid-column: span 3; }
}

@media (max-width: 560px) {
  .row, .holding-row, .fact-grid, .horizon-control { grid-template-columns: 1fr; }
  .header-tools { align-items: stretch; flex-direction: column; }
  .theme-control select { min-width: 0; }
  h1 { font-size: 24px; }
}
