/* Shared styles for the Korea Kontext presidential approval charts.
   Tokens mirror assets/css/kk.css (DESIGN.md). Charts add only series hues:
   one Flexoki hue per president, none red (reserved for Korea Kontext marks)
   and none blue (a party colour in Seoul). The sitting president is ink. */
:root {
  color-scheme: light dark;
  --paper: #FFFCF0;
  --paper-2: #F2F0E5;
  --rule: #DAD8CE;
  --rule-strong: #878580;
  --ink: #100F0F;
  --ink-2: #403E3C;
  --muted: #6F6E69;
  --red: #AF3029;
  --font-frame: "Hahmlet", "Apple SD Gothic Neo", "Noto Serif KR", Georgia, serif;
  /* Series. Light values pass 3:1 on paper. */
  --roh-tae-woo: #878580;
  --kim-young-sam: #AD8301;
  --kim-dae-jung: #24837B;
  --roh-moo-hyun: #66800B;
  --lee-myung-bak: #71320D;
  --park-geun-hye: #CB6120;
  --moon-jae-in: #5E409D;
  --yoon-suk-yeol: #A02F6F;
  --lee-jae-myung: var(--ink);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #100F0F;
    --paper-2: #1C1B1A;
    --rule: #343331;
    --rule-strong: #6F6E69;
    --ink: #CECDC3;
    --ink-2: #B7B5AC;
    --muted: #878580;
    --red: #D14D41;
    --roh-tae-woo: #878580;
    --kim-young-sam: #D0A215;
    --kim-dae-jung: #3AA99F;
    --roh-moo-hyun: #879A39;
    --lee-myung-bak: #EC8B49;
    --park-geun-hye: #CB6120;
    --moon-jae-in: #8B7EC8;
    --yoon-suk-yeol: #CE5D97;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #100F0F;
  --paper-2: #1C1B1A;
  --rule: #343331;
  --rule-strong: #6F6E69;
  --ink: #CECDC3;
  --ink-2: #B7B5AC;
  --muted: #878580;
  --red: #D14D41;
  --roh-tae-woo: #878580;
  --kim-young-sam: #D0A215;
  --kim-dae-jung: #3AA99F;
  --roh-moo-hyun: #879A39;
  --lee-myung-bak: #EC8B49;
  --park-geun-hye: #CB6120;
  --moon-jae-in: #8B7EC8;
  --yoon-suk-yeol: #CE5D97;
}

:root[data-theme="light"] { color-scheme: light; }

.static-mode .actions,
.static-mode .legend-tools,
.static-mode .chart-masthead,
.static-mode .readings-table { display: none; }
.page-controls .segmented { display: none; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-frame);
}

body { padding: clamp(16px, 3vw, 32px); }
.is-embedded body { padding: 0 0 4px; }

::selection { background: rgb(175 48 41 / 0.14); }

.chart-shell {
  width: min(100%, 1180px);
  margin: 0 auto;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------- Standalone masthead (hidden when framed by the site) ---------- */

.chart-masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 0 0 28px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--rule);
}
.is-embedded .chart-masthead { display: none; }
.chart-wordmark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-size: 1.25rem;
  font-weight: 800;
  text-decoration: none;
}
.chart-wordmark svg { width: 12px; height: 19px; fill: var(--red); }
.chart-masthead a { display: inline-flex; align-items: center; min-height: 2.75rem; }
.chart-masthead a:not(.chart-wordmark) {
  color: var(--ink);
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  text-decoration: none;
}
.chart-masthead a:hover { text-decoration: underline; text-decoration-color: var(--red); }

/* ---------- Header ---------- */

.chart-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 16px;
}

.chart-title {
  margin: 0 0 6px;
  font-size: clamp(1.375rem, 1.1rem + 1.2vw, 1.875rem);
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.01em;
  text-wrap: balance;
}
.is-embedded .chart-title { font-size: 1.375rem; }

.dek {
  max-width: 62ch;
  margin: 0;
  color: var(--ink-2);
  font-size: 0.9375rem;
  line-height: 1.5;
}

.actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
}

button, .button {
  min-height: 2.75rem;
  border: 1px solid var(--rule-strong);
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  white-space: nowrap;
}

.button:hover { border-color: var(--ink); }
button:focus-visible, .button:focus-visible, a:focus-visible { outline: 2px solid var(--red); outline-offset: 3px; }
button:disabled { cursor: progress; opacity: 0.6; }

.segmented {
  display: inline-flex;
  border: 1px solid var(--rule-strong);
}

.segmented button {
  min-height: calc(2.75rem - 2px);
  padding: 0 14px;
  border: 0;
  color: var(--muted);
}

.segmented button + button { border-left: 1px solid var(--rule-strong); }
.segmented button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.segmented button:hover:not([aria-pressed="true"]) { color: var(--ink); }

/* ---------- Legend and presets ---------- */

.legend-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 4px 24px;
  margin: 12px 0 0;
  padding: 6px 0;
  border-top: 1px solid var(--rule);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 6px;
  min-width: 0;
}

.legend button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  border: 0;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--ink);
}

.legend button:hover,
.legend button:focus-visible { background: var(--paper-2); }
.legend button[aria-pressed="false"] { color: var(--muted); }
.legend button[aria-pressed="false"] .legend-label { text-decoration: line-through; text-decoration-color: var(--muted); }
.legend button[aria-pressed="false"] .key { opacity: .35; }
.legend button.is-emphasized .legend-label { font-weight: 700; }

.key { display: block; flex: 0 0 auto; overflow: visible; }
.key line { stroke: var(--series-color); stroke-width: 2.5; stroke-linecap: round; }

.legend-tools {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 14px;
  font-size: 0.875rem;
}

.legend-tools button {
  padding: 0 2px;
  border: 0;
  color: var(--ink-2);
  font-weight: 500;
  text-decoration: underline;
  text-decoration-color: var(--rule-strong);
  text-underline-offset: 0.25em;
}

.legend-tools button:hover { color: var(--ink); text-decoration-color: var(--ink); }
.legend-tools button[aria-pressed="true"] {
  color: var(--ink);
  font-weight: 700;
  text-decoration-color: var(--ink);
  text-decoration-thickness: 2px;
}

.style-key {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
}

.style-key-item { display: inline-flex; align-items: center; gap: 8px; }

/* ---------- Plot and tooltip ---------- */

.plot-wrap {
  position: relative;
  width: 100%;
  min-height: 520px;
}

.plot {
  display: block;
  width: 100%;
  overflow: visible;
}
.plot text {
  font-family: var(--font-frame);
  font-variant-numeric: tabular-nums;
}

.tooltip {
  position: absolute;
  z-index: 4;
  width: min(280px, calc(100% - 16px));
  padding: 10px 12px 8px;
  border: 1px solid var(--rule-strong);
  background: var(--paper-2);
  color: var(--ink);
  pointer-events: none;
  opacity: 0;
  font-size: 0.8125rem;
  line-height: 1.35;
}
@media (prefers-reduced-motion: no-preference) { .tooltip { transition: opacity .08s ease-out; } }

.tooltip.visible { opacity: 1; }
.tooltip-title { margin-bottom: 7px; padding-bottom: 6px; border-bottom: 1px solid var(--rule); font-weight: 600; }
.tooltip-row { display: grid; grid-template-columns: 20px 1fr auto; gap: 8px; align-items: center; margin: 4px 0; }
.tooltip-row.is-emphasized .tooltip-name { font-weight: 700; }
.tooltip-row small { display: block; color: var(--muted); font-size: 0.75rem; }
.tooltip-value { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 0.875rem; }

/* ---------- Table alternative ---------- */

.readings-table { margin: 14px 0 0; padding-top: 4px; border-top: 1px solid var(--rule); }
.readings-table summary {
  display: inline-flex;
  align-items: center;
  min-height: 2.75rem;
  color: var(--ink-2);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: var(--rule-strong);
  text-underline-offset: 0.25em;
}
.readings-table summary:hover { color: var(--ink); text-decoration-color: var(--ink); }
.readings-table table {
  width: 100%;
  margin: 4px 0 8px;
  border-collapse: collapse;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}
.readings-table caption { padding: 0 0 8px; color: var(--muted); font-size: 0.8125rem; text-align: left; }
.readings-table th, .readings-table td { padding: 7px 10px 7px 0; border-bottom: 1px solid var(--rule); text-align: left; }
.readings-table thead th { border-bottom-color: var(--ink); }
.readings-table td.num, .readings-table th.num { text-align: right; padding-right: 0; }

/* ---------- Source ---------- */

.source {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px solid var(--rule);
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.5;
}

.source a { color: inherit; text-underline-offset: 2px; text-decoration-color: var(--rule-strong); }
.source a:hover { color: var(--ink); text-decoration-color: currentColor; }
.source-count { white-space: nowrap; }
.error { padding: 28px 0; color: var(--ink); }
.error a { color: inherit; }

/* ---------- Narrow frames and phones ---------- */

@media (max-width: 600px) {
  body { padding: 14px 16px; }
  .is-embedded body { padding: 0 0 4px; }
  .chart-header { display: block; }
  .actions { margin-top: 12px; flex-wrap: wrap; }
  .legend-wrap { display: block; }
  .source { display: block; }
  .source-count { display: block; margin-top: 5px; }
  .plot-wrap { min-height: 520px; }
  /* The plot comes straight after the title; the legend follows it. */
  .chart-shell { display: flex; flex-direction: column; }
  .chart-masthead { order: 0; }
  .chart-header { order: 1; }
  .plot-wrap { order: 2; }
  .style-key { order: 3; }
  .legend-wrap { order: 4; }
  .readings-table { order: 5; }
  .source { order: 6; }
}
