:root {
  --bg: #eef6ef;
  --card: rgba(255, 255, 255, 0.84);
  --line: #cfddce;
  --line-strong: #b8cab7;
  --ink: #173024;
  --muted: #607068;
  --accent: #c65d22;
  --accent-hover: #aa4d19;
  --positive: #1c8f56;
  --negative: #d34a38;
  --neutral-chip: #5272a8;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "IBM Plex Sans", "Source Sans 3", "Noto Sans SC", sans-serif;
  background:
    radial-gradient(circle at 14% 14%, rgba(122, 184, 125, 0.22), transparent 28%),
    radial-gradient(circle at 84% 86%, rgba(106, 143, 211, 0.18), transparent 30%),
    linear-gradient(180deg, #f7fbf7 0%, var(--bg) 100%);
}

.app {
  max-width: 1180px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.hero,
.card {
  background: var(--card);
  border: 1px solid rgba(207, 221, 206, 0.94);
  border-radius: 22px;
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: 0 20px 48px rgba(24, 46, 34, 0.08);
  backdrop-filter: blur(12px);
}

.hero h1 {
  margin: 0 0 8px;
  font-size: clamp(1.9rem, 4vw, 2.5rem);
  line-height: 1.08;
}

.hero p {
  margin: 0;
  max-width: 62ch;
  color: var(--muted);
}

.teach-card summary {
  font-weight: 700;
}

.teach-card p {
  margin-bottom: 0;
  color: var(--muted);
}

label {
  display: block;
  font-weight: 700;
  margin-bottom: 6px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

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

.input-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.field-help,
.control-tip,
.cloud-note,
.panel-head p {
  margin: 0;
  color: var(--muted);
  font-size: 0.94rem;
  line-height: 1.6;
}

textarea {
  width: 100%;
  min-height: 240px;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px 16px;
  font: inherit;
  line-height: 1.72;
  resize: vertical;
  background: rgba(255, 255, 255, 0.94);
}

select {
  min-width: 180px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 10px 40px 10px 14px;
  color: var(--ink);
  font: inherit;
  background:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%) calc(100% - 21px) calc(50% - 3px) / 7px 7px no-repeat,
    linear-gradient(135deg, var(--muted) 50%, transparent 50%) calc(100% - 16px) calc(50% - 3px) / 7px 7px no-repeat,
    rgba(255, 255, 255, 0.92);
  appearance: none;
}

.control-row {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  gap: 12px 18px;
  align-items: end;
  margin-top: 14px;
}

.range-group {
  display: grid;
  gap: 8px;
}

.range-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  font-size: 0.94rem;
  color: var(--muted);
}

output {
  font-weight: 700;
  color: var(--ink);
}

input[type="range"] {
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
  accent-color: var(--accent);
}

.actions {
  margin-top: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

button {
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  background: linear-gradient(135deg, var(--accent), #a54919);
}

button:hover {
  background: linear-gradient(135deg, var(--accent-hover), #954015);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.52;
  background: rgba(186, 196, 191, 0.92);
  color: rgba(23, 48, 36, 0.72);
  border-color: transparent;
  box-shadow: none;
}

.btn-secondary {
  border: 1px solid var(--line-strong);
  color: var(--ink);
  background: rgba(255, 255, 255, 0.85);
  box-shadow: none;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.96);
  color: var(--ink);
}

kbd {
  padding: 2px 6px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.9);
  font-family: "IBM Plex Mono", "Fira Code", monospace;
  font-size: 0.85em;
}

.status-line {
  margin: 12px 0 0;
  min-height: 1.5em;
  color: var(--muted);
}

.status-line.ok {
  color: var(--positive);
}

.status-line.warn {
  color: #9a6110;
}

.status-line.bad {
  color: var(--negative);
}

.output-card {
  padding: 20px;
}

.result-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 16px;
}

.result-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px 16px;
  align-items: center;
  margin-bottom: 16px;
}

.result-title h2,
.panel-head h3 {
  margin: 0;
}

.result-title p {
  margin: 6px 0 0;
  color: var(--muted);
}

.sentiment-legend {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(184, 202, 183, 0.9);
  font-size: 0.85rem;
  color: var(--muted);
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.legend-dot.positive {
  background: var(--positive);
}

.legend-dot.negative {
  background: var(--negative);
}

.legend-dot.neutral {
  background: var(--neutral-chip);
}

.filter-group,
.export-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.filter-chip {
  padding-inline: 14px;
}

.filter-chip.is-active {
  color: #fff;
  border-color: transparent;
  background: linear-gradient(135deg, var(--accent), #a54919);
}

.filter-chip.is-active:hover {
  background: linear-gradient(135deg, var(--accent-hover), #954015);
}

.result-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(280px, 0.9fr);
  gap: 18px;
  align-items: start;
}

.cloud-panel {
  display: grid;
  gap: 10px;
}

.cloud-shell {
  position: relative;
  min-height: 540px;
  padding: 14px;
  border-radius: 26px;
  border: 1px solid rgba(184, 202, 183, 0.92);
  background:
    radial-gradient(circle at 18% 16%, rgba(127, 186, 133, 0.18), transparent 25%),
    radial-gradient(circle at 84% 84%, rgba(90, 126, 200, 0.16), transparent 30%),
    linear-gradient(165deg, rgba(255, 255, 255, 0.95), rgba(245, 250, 246, 0.98));
  overflow: hidden;
}

.cloud-shell::before,
.cloud-shell::after {
  content: "";
  position: absolute;
  border-radius: 999px;
  pointer-events: none;
  opacity: 0.55;
}

.cloud-shell::before {
  width: 180px;
  height: 180px;
  top: -72px;
  right: -50px;
  background: radial-gradient(circle, rgba(94, 143, 212, 0.16), transparent 70%);
}

.cloud-shell::after {
  width: 220px;
  height: 220px;
  bottom: -88px;
  left: -60px;
  background: radial-gradient(circle, rgba(53, 153, 94, 0.14), transparent 72%);
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  margin: 0 auto;
  position: relative;
  z-index: 1;
}

.cloud-empty,
.fallback {
  position: absolute;
  inset: 16px;
  z-index: 2;
  border-radius: 18px;
}

.cloud-empty {
  display: grid;
  place-items: center;
  padding: 28px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed rgba(184, 202, 183, 0.85);
  background: rgba(255, 255, 255, 0.55);
}

.fallback {
  overflow: auto;
  border: 1px dashed rgba(184, 202, 183, 0.85);
  background: rgba(255, 255, 255, 0.9);
  padding: 16px;
}

.fallback-note {
  margin: 0 0 12px;
  font-size: 0.92rem;
  color: var(--muted);
}

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

.fallback-row,
.term-row {
  display: grid;
  grid-template-columns: minmax(0, auto) minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}

.fallback-label,
.term-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-weight: 600;
}

.fallback-label span:last-child,
.term-label span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fallback-track,
.term-track {
  height: 10px;
  border-radius: 999px;
  background: rgba(183, 196, 213, 0.3);
  overflow: hidden;
}

.fallback-fill,
.term-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--term-color, var(--neutral-chip));
}

.fallback-meta,
.term-meta {
  color: var(--muted);
  font-size: 0.84rem;
  font-variant-numeric: tabular-nums;
}

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

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

.metric-card,
.top-terms {
  border-radius: 20px;
  border: 1px solid rgba(184, 202, 183, 0.94);
  background: rgba(255, 255, 255, 0.86);
  box-shadow: 0 10px 24px rgba(24, 46, 34, 0.05);
}

.metric-card {
  padding: 14px;
}

.metric-label {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
}

.metric-value {
  display: block;
  margin-top: 10px;
  font-size: 1.45rem;
  line-height: 1.1;
}

.metric-value.positive {
  color: var(--positive);
}

.metric-value.negative {
  color: var(--negative);
}

.top-terms {
  padding: 16px;
}

.term-list {
  display: grid;
  gap: 11px;
  margin-top: 14px;
}

.term-dot,
.fallback-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  flex: none;
}

.term-empty {
  margin-top: 14px;
  color: var(--muted);
  font-size: 0.94rem;
}

.panel-head {
  display: grid;
  gap: 4px;
}

input:focus,
textarea:focus,
select:focus {
  box-shadow: 0 0 0 4px rgba(198, 93, 34, 0.12);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 920px) {
  .result-toolbar {
    grid-template-columns: 1fr;
  }

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

  .cloud-shell {
    min-height: 420px;
  }
}

@media (max-width: 640px) {
  body {
    background:
      radial-gradient(circle at 12% 10%, rgba(122, 184, 125, 0.2), transparent 24%),
      radial-gradient(circle at 88% 92%, rgba(106, 143, 211, 0.14), transparent 26%),
      linear-gradient(180deg, #f7fbf7 0%, var(--bg) 100%);
  }

  .app {
    padding: 12px 10px 28px;
  }

  .hero,
  .card {
    padding: 14px;
    border-radius: 18px;
  }

  .input-head,
  .result-head,
  .result-toolbar,
  .control-row {
    grid-template-columns: 1fr;
    display: grid;
  }

  .input-tools {
    justify-content: stretch;
  }

  .input-tools > * {
    width: 100%;
  }

  .sentiment-legend {
    justify-content: flex-start;
  }

  textarea {
    min-height: 220px;
  }

  .actions,
  .metric-grid,
  .filter-group,
  .export-group {
    grid-template-columns: 1fr 1fr;
  }

  .actions,
  .filter-group,
  .export-group {
    display: grid;
  }

  button,
  .btn-secondary,
  select {
    width: 100%;
    justify-content: center;
  }

  .cloud-shell {
    min-height: 360px;
    padding: 10px;
  }

  .metric-value {
    font-size: 1.25rem;
  }
}

:root[data-theme="dark"] {
  --bg: #16211c;
  --card: rgba(24, 33, 28, 0.88);
  --line: #30443a;
  --line-strong: #415849;
  --ink: #edf5ef;
  --muted: #a8bbb0;
  --accent: #ff8a47;
  --accent-hover: #ff9d67;
  --positive: #63d18d;
  --negative: #ff8677;
  --neutral-chip: #86a9ff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16211c;
    --card: rgba(24, 33, 28, 0.88);
    --line: #30443a;
    --line-strong: #415849;
    --ink: #edf5ef;
    --muted: #a8bbb0;
    --accent: #ff8a47;
    --accent-hover: #ff9d67;
    --positive: #63d18d;
    --negative: #ff8677;
    --neutral-chip: #86a9ff;
  }
}

:root[data-theme="dark"] .cloud-shell,
:root[data-theme="dark"] .metric-card,
:root[data-theme="dark"] .top-terms,
:root[data-theme="dark"] .legend-item,
:root[data-theme="dark"] select,
:root[data-theme="dark"] textarea,
:root[data-theme="dark"] .btn-secondary,
:root[data-theme="dark"] .cloud-empty,
:root[data-theme="dark"] .fallback {
  background-color: rgba(26, 35, 30, 0.88);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .cloud-shell,
  :root:not([data-theme="light"]) .metric-card,
  :root:not([data-theme="light"]) .top-terms,
  :root:not([data-theme="light"]) .legend-item,
  :root:not([data-theme="light"]) select,
  :root:not([data-theme="light"]) textarea,
  :root:not([data-theme="light"]) .btn-secondary,
  :root:not([data-theme="light"]) .cloud-empty,
  :root:not([data-theme="light"]) .fallback {
    background-color: rgba(26, 35, 30, 0.88);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* === Loading & Feedback States === */
@keyframes spin { to { transform: rotate(360deg); } }
.btn-loading { position: relative; pointer-events: none; opacity: 0.7; }
.btn-loading::after {
  content: '';
  position: absolute;
  right: 8px; top: 50%;
  width: 14px; height: 14px;
  margin-top: -7px;
  border: 2px solid transparent;
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
.toast-msg {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 20px; border-radius: 8px;
  font-size: 0.9rem; z-index: 9999; opacity: 0;
  transition: opacity 0.3s ease;
}
.toast-msg.show { opacity: 1; }
