:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --ink: #0f1b33;
  --muted: #53657d;
  --line: #dce4ee;
  --teal: #00a36f;
  --coral: #00bf83;
  --gold: #f59e0b;
  --green: #00a36f;
  --pale: #eef3f8;
  --sidebar: #0f172a;
  --shadow: 0 8px 18px rgba(15, 27, 51, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, Arial, sans-serif;
  letter-spacing: 0;
}

button,
select {
  font: inherit;
}

.app {
  width: min(1480px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 24px;
  align-items: end;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 8px;
  color: #16304f;
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  max-width: 900px;
  font-size: clamp(2rem, 4vw, 4.8rem);
  line-height: 1.04;
  font-weight: 900;
}

.subtitle {
  max-width: 780px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.7;
}

.source-note {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.source-note span {
  display: block;
  font-weight: 800;
}

.source-note small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  line-height: 1.5;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.controls label {
  display: grid;
  gap: 7px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.controls span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

select {
  width: 100%;
  min-height: 42px;
  border: 1px solid #cfd9e6;
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: #ffffff;
}

select:focus {
  outline: 3px solid rgba(0, 163, 111, 0.18);
  border-color: var(--teal);
}

.dashboard {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 16px;
  align-items: start;
}

.map-panel,
.insights section,
.stat-grid article {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.map-panel {
  grid-column: span 7;
  min-width: 0;
  padding: 16px;
}

.panel-head,
.section-title {
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
}

h2 {
  font-size: 1.05rem;
  font-weight: 900;
}

.panel-head p,
.section-title span {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.84rem;
}

.legend {
  display: grid;
  grid-template-columns: auto 110px auto;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 0.78rem;
}

.legend-ramp {
  height: 10px;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd45a, #ffa95a, #ff8b5a, #ff5a5a);
}

.map-shell {
  position: relative;
  height: clamp(420px, 56vh, 620px);
  min-height: 420px;
  margin-top: 12px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(rgba(15, 27, 51, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(15, 27, 51, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(15, 27, 51, 0.09) 1px, transparent 1px),
    linear-gradient(90deg, rgba(15, 27, 51, 0.09) 1px, transparent 1px),
    #eef3f8;
  background-size: 24px 24px, 24px 24px, 120px 120px, 120px 120px, auto;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none;
}

canvas.dragging {
  cursor: grabbing;
}

.zoom-controls {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 3;
  display: grid;
  grid-template-columns: 42px 42px 54px;
  gap: 6px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 12px 28px rgba(15, 27, 51, 0.14);
}

.zoom-controls button {
  min-width: 0;
  height: 36px;
  border: 1px solid #cfd9e6;
  border-radius: 6px;
  color: var(--ink);
  background: #ffffff;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
}

.zoom-controls button:hover {
  border-color: var(--teal);
  color: var(--teal);
}

.tooltip {
  position: absolute;
  z-index: 2;
  max-width: 260px;
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 18px));
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 16px 36px rgba(15, 27, 51, 0.16);
  color: var(--ink);
  font-size: 0.84rem;
  line-height: 1.45;
  opacity: 0;
  transition: opacity 120ms ease;
}

.tooltip.visible {
  opacity: 1;
}

.tooltip strong {
  display: block;
  margin-bottom: 4px;
}

.insights {
  display: contents;
}

.stat-grid {
  display: grid;
  grid-column: span 5;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.stat-grid article {
  min-height: 104px;
  padding: 16px;
}

.stat-grid span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.stat-grid strong {
  display: block;
  margin-top: 8px;
  font-size: clamp(1.35rem, 2.4vw, 2.2rem);
  line-height: 1.08;
}

.rank-panel,
.trend-panel,
.forecast-panel {
  grid-column: span 4;
  padding: 16px;
}

.rank-mode {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin-top: 14px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--pale);
}

.rank-mode[hidden] {
  display: none;
}

.rank-mode button {
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  font-weight: 800;
  cursor: pointer;
}

.rank-mode button.active {
  color: #fff;
  background: var(--teal);
  box-shadow: 0 6px 16px rgba(0, 163, 111, 0.22);
}

.rank-bars {
  display: grid;
  gap: 10px;
  margin: 14px 0 0;
  padding: 0;
}

.rank-column-chart {
  gap: 12px;
}

.rank-column-scroll {
  overflow-x: auto;
  padding: 4px 2px 8px;
}

.rank-column-grid {
  display: grid;
  grid-template-columns: repeat(10, minmax(38px, 1fr));
  gap: 8px;
  align-items: end;
  min-height: 260px;
}

.rank-column {
  display: grid;
  grid-template-rows: auto 180px auto;
  gap: 7px;
  min-width: 38px;
}

.rank-column-value {
  min-height: 32px;
  color: var(--ink);
  font-size: 0.68rem;
  font-weight: 900;
  line-height: 1.15;
  text-align: center;
  overflow-wrap: anywhere;
}

.rank-stack {
  position: relative;
  display: flex;
  align-items: flex-end;
  height: 180px;
  overflow: hidden;
  border-radius: 7px 7px 3px 3px;
  background:
    linear-gradient(rgba(53, 133, 142, 0.12) 1px, transparent 1px),
    var(--pale);
  background-size: 100% 25%;
}

.rank-stack-fill {
  display: flex;
  flex-direction: column-reverse;
  width: 100%;
  min-height: 2px;
  overflow: hidden;
  border-radius: inherit;
}

.rank-segment {
  display: block;
  min-height: 2px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.28);
}

.rank-column-label {
  display: grid;
  gap: 2px;
  min-width: 0;
  text-align: center;
}

.rank-column-label strong,
.rank-column-label span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-column-label strong {
  font-size: 0.74rem;
}

.rank-column-label span {
  color: var(--muted);
  font-size: 0.66rem;
}

.rank-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 10px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.rank-legend-item {
  display: inline-flex;
  gap: 5px;
  align-items: center;
}

.rank-legend-item i {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.rank-bar-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  min-height: 48px;
}

.rank-index {
  display: inline-grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 999px;
  color: #fff;
  background: var(--teal);
  font-size: 0.78rem;
  font-weight: 800;
}

.rank-name {
  min-width: 0;
}

.rank-bar-body {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.rank-bar-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}

.rank-name strong,
.rank-name span {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rank-name span {
  color: var(--muted);
  font-size: 0.78rem;
}

.rank-value {
  font-weight: 900;
  text-align: right;
  white-space: nowrap;
}

.rank-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--pale);
}

.rank-fill {
  height: 100%;
  min-width: 3px;
  border-radius: inherit;
  background: linear-gradient(90deg, #ffd45a, #ffa95a, #ff8b5a, #ff5a5a);
}

.empty-state {
  padding: 14px 0 2px;
  color: var(--muted);
  font-size: 0.88rem;
}

.bar-chart {
  display: grid;
  gap: 10px;
  margin-top: 16px;
}

.bar-row {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) 76px;
  gap: 10px;
  align-items: center;
  min-height: 26px;
  color: var(--muted);
  font-size: 0.82rem;
}

.bar-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--pale);
}

.bar-fill {
  height: 100%;
  min-width: 3px;
  border-radius: inherit;
  background: linear-gradient(90deg, #ffd45a, #ffa95a, #ff8b5a, #ff5a5a);
}

.bar-value {
  color: var(--ink);
  font-weight: 800;
  text-align: right;
}

.forecast-panel {
  display: grid;
  gap: 14px;
}

.forecast-title-actions {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  min-width: 0;
}

.hint-button {
  display: inline-flex;
  min-height: 32px;
  max-width: 100%;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 14px;
  border: 0;
  border-radius: 7px;
  color: #ffffff;
  background: var(--teal);
  box-shadow: none;
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1.2;
  text-align: center;
  cursor: pointer;
  transition: background 120ms ease, box-shadow 120ms ease, transform 120ms ease;
}

.hint-button::before {
  content: "";
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
  background:
    linear-gradient(#ffffff 0 0) 0 0 / 5px 5px no-repeat,
    linear-gradient(#ffffff 0 0) 9px 0 / 5px 5px no-repeat,
    linear-gradient(#ffffff 0 0) 0 9px / 5px 5px no-repeat,
    linear-gradient(#ffffff 0 0) 9px 9px / 5px 5px no-repeat;
  opacity: 0.95;
}

.hint-button:hover,
.hint-button:focus-visible,
.hint-button[aria-expanded="true"] {
  color: #ffffff;
  background: #009665;
  box-shadow: 0 8px 18px rgba(0, 163, 111, 0.2);
}

.hint-button:hover {
  transform: translateY(-1px);
}

.hint-button:focus-visible {
  outline: 3px solid rgba(0, 163, 111, 0.18);
  outline-offset: 2px;
}

#forecastFormulaToggle {
  min-width: 154px;
  min-height: 31px;
  padding: 7px 14px;
  border: 0;
  border-radius: 7px;
  color: #ffffff;
  background: #009f6b;
  box-shadow: none;
}

#forecastFormulaToggle::before {
  content: "";
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
  background:
    linear-gradient(#ffffff 0 0) 0 0 / 5px 5px no-repeat,
    linear-gradient(#ffffff 0 0) 9px 0 / 5px 5px no-repeat,
    linear-gradient(#ffffff 0 0) 0 9px / 5px 5px no-repeat,
    linear-gradient(#ffffff 0 0) 9px 9px / 5px 5px no-repeat;
}

#forecastFormulaToggle:hover,
#forecastFormulaToggle:focus-visible,
#forecastFormulaToggle[aria-expanded="true"] {
  color: #ffffff;
  background: #009f6b;
  box-shadow: none;
}

.forecast-notice {
  padding: 12px 14px;
  border: 1px solid rgba(245, 158, 11, 0.34);
  border-radius: 8px;
  color: #74460a;
  background: #fff7e6;
  font-size: 0.86rem;
  font-weight: 800;
  line-height: 1.55;
}

.forecast-formula {
  display: grid;
  gap: 13px;
  padding: 14px;
  border: 1px solid rgba(0, 163, 111, 0.24);
  border-radius: 8px;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(241, 251, 247, 0.94), rgba(255, 255, 255, 0.94)),
    #ffffff;
  font-size: 0.8rem;
  line-height: 1.62;
}

.forecast-formula[hidden] {
  display: none;
}

.forecast-formula strong {
  font-size: 0.92rem;
}

.formula-copy {
  display: grid;
  gap: 10px;
}

.formula-copy p {
  margin: 0;
  padding: 11px 12px 11px 14px;
  overflow-wrap: anywhere;
  border: 1px solid rgba(15, 27, 51, 0.08);
  border-left: 4px solid var(--teal);
  border-radius: 8px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 6px 14px rgba(15, 27, 51, 0.05);
}

.formula-copy p::first-line {
  color: var(--ink);
  font-size: 0.86rem;
  font-weight: 900;
}

.formula-copy p:last-child {
  border-left-color: var(--gold);
  background: #fff7e6;
}

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

.forecast-summary article {
  min-height: 78px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--pale);
}

.forecast-summary span,
.forecast-list-head span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
}

.forecast-summary strong {
  display: block;
  margin-top: 7px;
  font-size: 1.28rem;
  line-height: 1.1;
}

.forecast-list-head {
  display: flex;
  gap: 10px;
  align-items: end;
  justify-content: space-between;
  min-width: 0;
}

.forecast-list-head h3 {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.25;
}

.forecast-list {
  display: grid;
  gap: 10px;
}

.forecast-item {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  min-height: 72px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.forecast-item:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.forecast-rank {
  display: inline-grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 999px;
  color: #fff;
  background: var(--sidebar);
  font-size: 0.78rem;
  font-weight: 900;
}

.forecast-item-body {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.forecast-item-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: start;
}

.forecast-item-head strong {
  overflow: hidden;
  font-size: 0.94rem;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.forecast-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  line-height: 1.45;
}

.risk-badge {
  min-width: 72px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 900;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
}

.risk-high {
  color: #7f1d1d;
  background: #fee2d5;
}

.risk-medium {
  color: #74460a;
  background: #fef3c7;
}

.risk-low {
  color: #14532d;
  background: #dcfce7;
}

.risk-neutral {
  color: var(--muted);
  background: var(--pale);
}

@media (max-width: 1080px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .source-note {
    width: min(100%, 520px);
  }

  .map-panel,
  .stat-grid,
  .rank-panel,
  .trend-panel,
  .forecast-panel {
    grid-column: span 12;
  }

  .map-shell {
    height: clamp(460px, 62vh, 620px);
  }
}

@media (max-width: 720px) {
  .app {
    width: min(100% - 20px, 1480px);
    padding: 18px 0;
  }

  .controls,
  .stat-grid,
  .forecast-summary {
    grid-template-columns: 1fr;
  }

  .panel-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .map-shell {
    min-height: 480px;
    height: 62vh;
  }

  .bar-row {
    grid-template-columns: 38px minmax(0, 1fr) 66px;
  }

  .rank-mode {
    grid-template-columns: 1fr;
  }

  .rank-bar-head {
    grid-template-columns: 1fr;
    gap: 2px;
  }

  .rank-value {
    text-align: left;
  }

  .forecast-list-head,
  .forecast-item-head {
    align-items: start;
    grid-template-columns: 1fr;
  }

  .forecast-list-head {
    flex-direction: column;
  }

  .forecast-title-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }
}
