/* Essay page, visually aligned with the kindlyanson.com article pages.
   Colors, type, and header are local so this GitHub Pages site does not
   depend on kindlyanson.com stylesheets. */

:root,
[data-theme="light"] {
  --bg: #f4f3ef;
  --surface: #fbfaf7;
  --surface-2: #f1eee8;
  --s2: #edeae4;
  --text: #28251d;
  --muted: #7a7974;
  --border: oklch(0.2 0.01 80 / 0.12);
  --primary: #01696f;
  --primary-hover: #0c4e54;
  --shadow: 0 12px 32px oklch(0.2 0.01 80 / 0.1);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #111214;
  --surface: #17191d;
  --surface-2: #1d2026;
  --s2: #1d2026;
  --text: #e2e6ee;
  --muted: #95a0b3;
  --border: rgba(255, 255, 255, 0.08);
  --primary: #4ea1ff;
  --primary-hover: #2d8eff;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  min-height: 100%;
}

body {
  font-family: "Satoshi", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  background:
    radial-gradient(circle at top right, rgba(78, 161, 255, 0.1), transparent 28%),
    radial-gradient(circle at bottom left, rgba(1, 105, 111, 0.1), transparent 24%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
}

.topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in oklab, var(--surface) 86%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  min-width: max-content;
  text-decoration: none;
  color: inherit;
}

.brand-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: max-content;
}

.brand-name {
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0;
  line-height: 1.2;
  white-space: nowrap;
}

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

.topbar .btn {
  background: var(--s2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font: inherit;
  font-weight: 500;
  font-size: 12px;
  min-height: 34px;
  cursor: pointer;
}

.topbar .btn:hover,
.topbar .btn:active {
  transform: none;
}

a:focus-visible,
.topbar .btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 3px;
}

.essay {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 36px 24px 56px;
  text-align: left;
}

.back-link {
  display: inline-block;
  margin: 0 0 18px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-decoration: none;
}

.back-link:hover {
  color: var(--primary);
}

.cover-avatar {
  display: block;
  width: 96px;
  height: 96px;
  margin: 0 0 18px;
  border-radius: 50%;
  object-fit: cover;
  object-position: center;
  background: var(--s2);
}

.essay h1 {
  margin: 0 0 12px;
  max-width: 18em;
  font-size: clamp(32px, 4.5vw, 48px);
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.03em;
}

.dek {
  margin: 0;
  max-width: 22em;
  font-size: clamp(18px, 2.2vw, 22px);
  font-weight: 500;
  line-height: 1.55;
  letter-spacing: -0.02em;
  color: var(--muted);
}

.essay-body {
  display: grid;
  gap: 28px;
  margin-top: 28px;
}

.essay-body p {
  margin: 0;
  max-width: 40em;
  font-size: 18px;
  line-height: 1.85;
  color: var(--muted);
}

.essay-body p + p {
  margin-top: 10px;
}

.chart-figure {
  margin: 0;
  width: 100%;
  min-width: 0;
}

.chart-card {
  --bg: #0b0e13;
  --bg-elev: #141922;
  --bg-hover: #1c2330;
  --border: #2a3140;
  --text: #d7dde8;
  --muted: #8b93a7;
  --accent: #3d8bfd;
  --accent-dim: #2a5a9e;
  --up: #26a69a;
  --down: #ef5350;
  --danger: #e57373;
  --ema10: #f5d76e;
  --ema20: #5dade2;
  --sma50: #c39bd3;
  --sma200: #e59866;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
  color-scheme: dark;
  font-family: "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  font-size: 13px;
}

.chart-card .toolbar {
  position: relative;
  z-index: 4;
  flex: 0 0 auto;
  padding: 10px 12px 8px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chart-card .row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  min-height: 28px;
}

.chart-card label.field {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  white-space: nowrap;
  min-width: 0;
}

.chart-card select,
.chart-card button,
.chart-card input[type="text"] {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.chart-card select,
.chart-card button {
  height: 32px;
  padding: 0 10px;
}

.chart-card select {
  max-width: 280px;
  min-width: 0;
}

.chart-card select#case-select {
  min-width: 148px;
  max-width: 320px;
}

.chart-card button {
  cursor: pointer;
  background: #1a2130;
  white-space: nowrap;
}

.chart-card button:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: #3a4558;
}

.chart-card button:focus-visible,
.chart-card select:focus-visible,
.chart-card input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.chart-card button:disabled {
  opacity: 0.4;
  cursor: default;
}

.chart-card button.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}

.chart-card .counter {
  color: var(--muted);
  margin-left: 2px;
  min-width: 8.5em;
  font-variant-numeric: tabular-nums;
}

.chart-card .draw-tools {
  flex-wrap: wrap;
  gap: 4px;
}

.chart-card .draw-tools button {
  flex: 0 0 auto;
  padding: 0 8px;
}

.chart-card .download-menu-wrap {
  position: relative;
  display: inline-flex;
  flex: 0 0 auto;
}

.chart-card .download-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 5;
  min-width: max-content;
  padding: 4px;
  background: #141a24;
  border: 1px solid #2a3140;
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}

.chart-card .download-menu[hidden] {
  display: none;
}

.chart-card .draw-tools .download-menu button {
  display: block;
  width: 100%;
  height: auto;
  min-height: 32px;
  margin: 0;
  padding: 6px 10px;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 4px;
  white-space: nowrap;
}

.chart-card .download-menu button:hover:not(:disabled) {
  background: #243044;
  border-color: transparent;
}

.chart-card .legend {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  flex-wrap: wrap;
  min-width: 0;
}

.chart-card .legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.chart-card .swatch {
  width: 10px;
  height: 2px;
  border-radius: 1px;
  display: inline-block;
}

.chart-card .swatch.ema10 { background: var(--ema10); }
.chart-card .swatch.ema20 { background: var(--ema20); }
.chart-card .swatch.sma50 { background: var(--sma50); }
.chart-card .swatch.sma200 { background: var(--sma200); }
.chart-card .swatch.vol {
  background: #5c677a;
  height: 8px;
  width: 8px;
  border-radius: 2px;
}

.chart-card .legend .val {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  font-style: normal;
}

.chart-card .ohlc {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin-left: 4px;
  white-space: normal;
}

.chart-card .ohlc b {
  font-weight: 600;
  color: var(--text);
}

.chart-card .ohlc.up b { color: var(--up); }
.chart-card .ohlc.down b { color: var(--down); }

.chart-card #status {
  min-height: 0;
  color: var(--muted);
  padding: 0 2px;
  line-height: 1.45;
}

.chart-card #status.error { color: var(--danger); }
.chart-card #status.info { color: #9ec1ff; }

.chart-card #chart-wrap {
  position: relative;
  height: clamp(440px, 68vh, 640px);
  min-height: 440px;
  background: #0b0e13;
}

.chart-card #chart {
  width: 100%;
  height: 100%;
  touch-action: pan-y;
}

.chart-card #note-editor {
  position: absolute;
  z-index: 20;
  display: none;
  transform: translate(8px, -50%);
  max-width: calc(100% - 16px);
}

.chart-card #note-editor.visible {
  display: block;
}

.chart-card #note-editor input {
  width: min(240px, 100%);
  height: 36px;
  padding: 0 10px;
  background: #1b2230;
  border: 1px solid var(--accent);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  outline: none;
}

.chart-card #boot-error {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: var(--danger);
  background: #0b0e13;
  z-index: 30;
}

.chart-card #boot-error.visible {
  display: flex;
}

.chart-card.is-drawing #chart {
  touch-action: none;
}

.draw-panel {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: #10151e;
  padding: 8px 12px 10px;
  max-height: 168px;
  overflow: auto;
}

.draw-panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
}

.draw-panel-head strong {
  font-size: 13px;
  font-weight: 650;
}

.draw-saved {
  color: #9ec1ff;
  font-size: 12px;
  min-height: 1em;
}

.draw-actions {
  margin-left: auto;
  display: inline-flex;
  gap: 8px;
  flex-shrink: 0;
}

.draw-empty {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.draw-empty[hidden] {
  display: none;
}

.draw-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 6px;
}

.draw-list:empty {
  display: none;
}

.draw-list li {
  display: flex;
  align-items: stretch;
  gap: 6px;
  min-width: 0;
}

.chart-card .draw-list button.draw-item {
  flex: 1 1 auto;
  height: auto;
  min-height: 32px;
  padding: 4px 8px;
  text-align: left;
  white-space: normal;
  line-height: 1.4;
  background: transparent;
}

.chart-card .draw-list li.selected button.draw-item {
  border-color: var(--accent);
  color: #fff;
}

.chart-card .draw-list button.draw-remove {
  flex: 0 0 auto;
}

body.fs-lock {
  overflow: hidden;
}

.chart-card:fullscreen,
.chart-card:-webkit-full-screen,
.chart-card.is-fs-fallback {
  border-radius: 0;
  width: 100%;
  height: 100dvh;
  max-height: none;
  display: flex;
  flex-direction: column;
  background: #0b0e13;
  overflow: hidden;
}

.chart-card.is-fs-fallback {
  position: fixed;
  inset: 0;
  z-index: 80;
}

.chart-card:fullscreen #chart-wrap,
.chart-card:-webkit-full-screen #chart-wrap,
.chart-card.is-fs-fallback #chart-wrap {
  flex: 1 1 0;
  height: 0;
  min-height: 0;
}

.chart-card:fullscreen .draw-panel,
.chart-card:-webkit-full-screen .draw-panel,
.chart-card.is-fs-fallback .draw-panel {
  max-height: 28vh;
}

.footer {
  padding: 18px 22px 26px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

.footer a {
  color: inherit;
  text-decoration: none;
}

.footer a:hover {
  color: var(--primary);
}

@media (max-width: 760px) {
  .topbar {
    padding: 8px 12px;
  }

  .chart-card label.field {
    flex: 1 1 calc(50% - 8px);
  }

  .chart-card label.field select {
    flex: 1 1 auto;
    width: 100%;
    max-width: none;
  }

  .chart-card select#case-select {
    min-width: 0;
    max-width: none;
  }
}

@media (max-width: 520px) {
  .essay {
    padding: 24px 14px 40px;
  }

  .topbar .btn {
    min-height: 44px;
    padding: 0 14px;
  }

  .chart-card .toolbar {
    padding: 10px 10px 8px;
  }

  .chart-card select,
  .chart-card button {
    height: 40px;
    padding: 0 12px;
  }

  .chart-card .draw-tools {
    flex-wrap: wrap;
    gap: 4px;
  }

  .chart-card .draw-tools button {
    height: 36px;
    padding: 0 8px;
  }

  .chart-card label.field {
    flex: 1 1 100%;
  }

  .chart-card #chart-wrap {
    height: clamp(460px, 74vh, 640px);
    min-height: 460px;
  }

  .chart-card .legend {
    gap: 8px 10px;
  }

  .chart-card .counter {
    min-width: max-content;
  }

  .draw-panel-head {
    flex-wrap: wrap;
  }

  .draw-actions {
    margin-left: 0;
  }

  .chart-card .draw-list button.draw-item,
  .chart-card .draw-list button.draw-remove {
    min-height: 40px;
  }
}

@media (max-width: 420px) {
  .brand-name {
    font-size: 13px;
  }
}

/* Chart-only embed (embed.html, and index.html?embed=1).
   The page stays dark and flush so a cross-origin iframe on either
   kindlyanson.com theme does not show essay chrome or a light gap. */
html.is-embed {
  height: 100%;
  background: #0b0e13;
  color-scheme: dark;
}

html.is-embed body {
  height: 100%;
  min-height: 100%;
  margin: 0;
  overflow: auto;
  background: #0b0e13;
  color: #d7dde8;
}

html.is-embed .topbar,
html.is-embed .footer,
html.is-embed .back-link,
html.is-embed .cover-avatar,
html.is-embed .essay > h1,
html.is-embed .dek,
html.is-embed .essay-body > section {
  display: none !important;
}

html.is-embed main,
html.is-embed .essay,
html.is-embed .essay-body,
html.is-embed .chart-figure {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  width: 100%;
  max-width: none;
  height: 100%;
  min-height: 100%;
  margin: 0;
  padding: 0;
  gap: 0;
}

html.is-embed .chart-card {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  min-height: 100%;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

html.is-embed .chart-card #chart-wrap {
  flex: 1 1 0;
  height: auto;
  min-height: 220px;
}

html.is-embed .draw-panel {
  flex: 0 0 auto;
  max-height: min(168px, 30vh);
}

@media (max-width: 520px) {
  html.is-embed .essay {
    padding: 0;
  }

  html.is-embed .chart-card #chart-wrap {
    height: auto;
    min-height: 220px;
  }
}
