:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --panel-2: #f1f5f9;
  --border: #cbd5e1;
  --border-light: #e2e8f0;
  --text: #0f172a;
  --text-dim: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --good: #059669;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.4;
}

header {
  padding: 16px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
}

header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

main {
  padding: 20px 24px;
  display: grid;
  grid-template-columns: minmax(380px, 420px) 1fr;
  gap: 20px;
  max-width: 1600px;
  margin: 0 auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
  min-width: 0;
}

.panel h2 {
  margin: 0 0 12px 0;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.panel-header h2 { margin: 0; }

.chart-panel { grid-column: 2; }
.panel:nth-of-type(3) { grid-column: 1 / -1; }

.chart-wrap {
  position: relative;
  height: 380px;
  min-width: 0;
}

.chart-wrap canvas {
  max-width: 100%;
}

/* Tabs */
.tabs-wrap {
  display: flex;
  align-items: stretch;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.series-tabs {
  display: flex;
  align-items: stretch;
  gap: 2px;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}

.series-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  padding: 8px 12px;
  margin-bottom: -1px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
  transition: background 0.1s, color 0.1s;
  flex-shrink: 0;
}

.series-tab:hover {
  background: var(--panel-2);
  color: var(--text);
}

.series-tab.active {
  background: var(--panel);
  border-color: var(--border);
  border-bottom-color: var(--panel);
  color: var(--text);
  font-weight: 600;
}

.series-tab.disabled .tab-name {
  text-decoration: line-through;
  opacity: 0.6;
}

.tab-add {
  padding: 4px 10px;
  font-size: 16px;
  line-height: 1;
  align-self: center;
  margin-bottom: 4px;
  flex-shrink: 0;
}

/* Series editor */
.series-editor {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.editor-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.series-name {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  padding: 6px 8px;
  border-radius: 4px;
  min-width: 0;
}

.series-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.series-grid label {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  gap: 4px;
  font-weight: 500;
}

.hint {
  margin: 4px 0 0;
  font-size: 11px;
  color: var(--text-dim);
  font-style: italic;
}

/* Form controls */
input[type="number"], input[type="text"], select {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 8px;
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
}

input[type="number"]:focus, input[type="text"]:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

input[type="color"] {
  width: 100%;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--panel);
  padding: 2px;
  cursor: pointer;
}

input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

.color-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.1);
}

label.inline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
}

label.inline input[type="number"] {
  width: 80px;
}

label.inline input[type="text"] {
  width: 56px;
  text-align: center;
}

label.checkbox {
  cursor: pointer;
  user-select: none;
}

label.checkbox.tight { gap: 4px; }

/* Buttons */
.btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  transition: background 0.1s, border-color 0.1s;
}

.btn:hover {
  background: var(--panel-2);
  border-color: var(--text-dim);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn.icon {
  padding: 2px 8px;
  font-size: 18px;
  line-height: 1;
  color: var(--text-dim);
}

.btn.icon:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--panel);
}

/* Table */
.table-wrap {
  overflow-x: auto;
  max-height: 500px;
  overflow-y: auto;
  border: 1px solid var(--border-light);
  border-radius: 4px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

th, td {
  padding: 6px 10px;
  text-align: right;
  border-bottom: 1px solid var(--border-light);
}

th:first-child, td:first-child {
  text-align: left;
  position: sticky;
  left: 0;
  background: var(--panel);
  z-index: 1;
  font-weight: 500;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--panel-2);
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  font-size: 11px;
  z-index: 2;
}

thead tr.series-group-row th {
  text-align: center;
  border-bottom: 2px solid var(--border);
  border-left: 1px solid var(--border-light);
  color: var(--text);
}

thead tr.series-group-row th:first-child {
  border-left: none;
  background: var(--panel-2);
}

thead tr.col-row th {
  top: 30px;
  font-weight: 500;
}

tbody tr:nth-child(even) td { background: var(--panel-2); }
tbody tr:nth-child(even) td:first-child { background: #e9eef5; }
tbody tr:hover td { background: rgba(37, 99, 235, 0.08); }
tbody tr:hover td:first-child { background: rgba(37, 99, 235, 0.12); }

.col-group-divider { border-left: 1px solid var(--border-light); }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--good);
  color: white;
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.toast.show { opacity: 1; }

.chart-error {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-dim);
  font-style: italic;
}

/* Ad slots — hidden until enabled.
   To turn ads back on, remove `display: none` from .ad-slot below. */
.ad-slot {
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--panel-2);
  border: 1px dashed var(--border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 16px auto;
  overflow: hidden;
}

.ad-leaderboard {
  width: 100%;
  max-width: 728px;
  height: 90px;
  margin: 16px auto;
}

.ad-fullwidth { grid-column: 1 / -1; }

/* Header tagline */
.header-title { display: flex; flex-direction: column; gap: 2px; }
.header-title .tagline {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
}

/* Long-form educational content */
.content {
  grid-column: 1 / -1;
  background: var(--panel);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 24px 28px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  min-width: 0;
}

.content h2 {
  margin: 28px 0 10px;
  font-size: 20px;
  font-weight: 700;
  text-transform: none;
  letter-spacing: normal;
  color: var(--text);
}

.content h2:first-child { margin-top: 0; }

.content h3 {
  margin: 20px 0 8px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.content p { margin: 0 0 12px; }

.content ul.legend {
  margin: 0 0 14px;
  padding-left: 20px;
}

.content ul.legend li { margin-bottom: 4px; }

.content .formula {
  background: var(--panel-2);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 12px 16px;
  font-size: 16px;
  text-align: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.content code { font-family: inherit; }

.content ul.reading {
  margin: 0 0 14px;
  padding-left: 20px;
}
.content ul.reading li { margin-bottom: 8px; }
.content a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}
.content a:hover { color: var(--accent-hover); }

.content .example-card,
.content .example-card:hover {
  text-decoration: none;
  color: var(--text);
}

.examples {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
  margin: 6px 0 18px;
}

.example-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  text-decoration: none;
  transition: border-color 0.1s, background 0.1s, transform 0.1s;
}

.example-card:hover {
  border-color: var(--accent);
  background: var(--panel);
  transform: translateY(-1px);
}

.example-card strong {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.example-card span {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.45;
}

.worked-link {
  font-size: 13.5px;
  white-space: nowrap;
}

.site-footer {
  text-align: center;
  padding: 24px 16px 32px;
  font-size: 13px;
  color: var(--text-dim);
}

.site-footer a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.1s;
}

.site-footer a:hover { border-bottom-color: var(--accent); }

.header-link {
  font-size: 13px;
  color: var(--text-dim);
  text-decoration: none;
  border-bottom: 1px dashed var(--border);
  padding-bottom: 1px;
}

.header-link:hover {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.ad-rectangle {
  width: 100%;
  max-width: 300px;
  height: 250px;
  margin: 16px auto 0;
}

.ad-placeholder {
  user-select: none;
  pointer-events: none;
}

@media (max-width: 820px) {
  main { grid-template-columns: 1fr; }
  .chart-panel { grid-column: 1; }
  .ad-rectangle { margin: 16px auto; }

  header {
    padding: 14px 16px;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  .header-title { width: 100%; }
  header h1 { font-size: 17px; }
  .header-title .tagline { font-size: 12.5px; }
  .header-controls {
    width: 100%;
    gap: 12px;
    row-gap: 10px;
  }
  .header-controls .btn { margin-left: auto; }

  main {
    padding: 14px 16px;
    gap: 14px;
  }

  .panel { padding: 14px; }
  .chart-wrap { height: 280px; }

  .content {
    padding: 20px 18px;
  }
  .content h2 { font-size: 18px; margin-top: 22px; }
  .content h3 { font-size: 15px; margin-top: 16px; }
  .content { font-size: 14.5px; }

  .examples { grid-template-columns: 1fr; }

  .series-tab { padding: 8px 10px; min-height: 36px; }
  .btn { min-height: 36px; padding: 6px 14px; }

  th, td { padding: 5px 8px; }
  thead tr.col-row th { top: 28px; }

  .site-footer { padding: 16px 16px 24px; }
}

@media (max-width: 520px) {
  body { font-size: 13.5px; }

  header { padding: 12px 12px; }
  header h1 { font-size: 16px; }

  .header-controls {
    flex-wrap: wrap;
    gap: 10px;
    column-gap: 14px;
  }
  .header-controls .header-link {
    order: -1;
    flex-basis: 100%;
  }
  .header-controls .btn {
    margin-left: 0;
    width: 100%;
  }
  label.inline {
    font-size: 12.5px;
    flex: 1 1 0;
    min-width: 0;
  }
  label.inline input[type="number"],
  label.inline input[type="text"] {
    width: 100%;
    flex: 1 1 0;
    min-width: 0;
  }

  main {
    padding: 12px;
    gap: 12px;
  }

  .panel { padding: 12px; border-radius: 6px; }

  .series-grid { grid-template-columns: 1fr; gap: 8px; }

  .chart-wrap { height: 240px; }

  .content { padding: 16px 14px; font-size: 14px; }
  .content h2 { font-size: 17px; margin-top: 20px; }
  .content .formula { font-size: 14px; padding: 10px 12px; }

  .example-card { padding: 12px 14px; }

  th, td { padding: 4px 6px; font-size: 11.5px; }

  .toast {
    bottom: 16px;
    font-size: 12.5px;
    padding: 8px 14px;
  }
}
