/* ============================================================
   TEXT & WRITING TOOLS — text-writer.css
   Powers the .tw2 tool block shared by all 16 text tools
   (text-writer.js). Pairs with tools.css (cat-content) and
   the global tokens from main.css.
   ============================================================ */

/* ── Section shell ─────────────────────────── */
.tw2 {
  max-width: 1100px;
  margin: -4rem auto 4rem;
  padding: 0 1.25rem;
  position: relative;
  z-index: 2;
}

.tw2__panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  padding: 1.75rem;
  overflow: hidden;
}

.tw2__header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.tw2__title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 700;
}

.tw2__badge {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--primary) 30%, transparent);
  padding: 0.2rem 0.6rem;
  border-radius: var(--radius-full);
  white-space: nowrap;
}

/* ── Inputs & outputs ─────────────────────── */
.tw2-input,
.tw2-output {
  width: 100%;
  min-height: 180px;
  resize: vertical;
  font: inherit;
  font-size: 0.95rem;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.9rem 1rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.tw2-input:focus,
.tw2-output:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.tw2-output {
  background: var(--bg-secondary);
  min-height: 150px;
}

.tw2-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.tw2-col__label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin: 0 0 0.4rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

/* ── Count bar ────────────────────────────── */
.tw2-count {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.6rem 0 0;
}

.tw2-count__chip {
  font-size: 0.78rem;
  color: var(--text-secondary);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 0.2rem 0.7rem;
}

.tw2-count__chip strong { color: var(--text-primary); }

/* ── Options row ──────────────────────────── */
.tw2-options {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
  margin: 1rem 0;
}

.tw2-opt {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.tw2-opt label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.tw2-opt select,
.tw2-opt input[type="number"],
.tw2-opt input[type="text"],
.tw2-opt input[type="range"] {
  font: inherit;
  font-size: 0.9rem;
  color: var(--text-primary);
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.45rem 0.7rem;
  min-width: 140px;
}

.tw2-opt input[type="range"] { padding: 0; min-width: 180px; }

.tw2-opt select:focus,
.tw2-opt input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.tw2-opt--check {
  flex-direction: row;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.9rem;
  color: var(--text-primary);
  min-height: 38px;
}

.tw2-opt--check input { accent-color: var(--primary); width: 16px; height: 16px; }

/* ── Action buttons ───────────────────────── */
.tw2-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 1rem;
}

.tw2-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  padding: 0.55rem 1.1rem;
  cursor: pointer;
  border: 1px solid transparent;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
}

.tw2-btn:active { transform: translateY(1px); }

.tw2-btn--primary {
  color: var(--white);
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  box-shadow: var(--shadow-md);
}

.tw2-btn--primary:hover { box-shadow: var(--shadow-glow); }

.tw2-btn--ghost {
  color: var(--text-primary);
  background: var(--bg-secondary);
  border-color: var(--border);
}

.tw2-btn--ghost:hover { background: var(--surface-hover); }

.tw2-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Stat cards ───────────────────────────── */
.tw2-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 0.7rem;
  margin-bottom: 1rem;
}

.tw2-stat {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.9rem 1rem;
  text-align: center;
}

.tw2-stat__value {
  display: block;
  font-size: 1.5rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1.2;
}

.tw2-stat__label {
  display: block;
  font-size: 0.78rem;
  color: var(--text-secondary);
  margin-top: 0.2rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.tw2-stat--wide { grid-column: span 2; }

/* ── Diff / compare ───────────────────────── */
.tw2-diff {
  max-height: 420px;
  overflow: auto;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-top: 1rem;
  font-size: 0.9rem;
}

.tw2-diff-row {
  display: flex;
  gap: 0.6rem;
  padding: 0.35rem 0.8rem;
  border-bottom: 1px solid var(--border-light);
}

.tw2-diff-row:last-child { border-bottom: none; }

.tw2-diff-row--added { background: color-mix(in srgb, var(--success) 12%, transparent); }
.tw2-diff-row--removed { background: color-mix(in srgb, var(--error) 12%, transparent); text-decoration: line-through; }
.tw2-diff-row--same { color: var(--text-secondary); }

.tw2-diff-mark {
  flex: 0 0 1.1rem;
  font-weight: 800;
  text-align: center;
  color: var(--text-secondary);
}

.tw2-diff-row--added .tw2-diff-mark { color: var(--success); }
.tw2-diff-row--removed .tw2-diff-mark { color: var(--error); }

.tw2-diff-text { white-space: pre-wrap; word-break: break-word; }

/* ── Similarity ───────────────────────────── */
.tw2-sim-list {
  margin: 1rem 0 0;
  padding: 0.9rem 1rem 0.9rem 2.2rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  max-height: 260px;
  overflow: auto;
}

.tw2-sim-list li { margin-bottom: 0.5rem; font-size: 0.9rem; }
.tw2-sim-list li:last-child { margin-bottom: 0; }
.tw2-sim-none { list-style: none; margin-left: -1.2rem; color: var(--text-secondary); }

/* ── Invisible char preview ───────────────── */
.tw2-invis-preview {
  margin-top: 0.8rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
  padding: 0.6rem 0.9rem;
  word-break: break-all;
}

/* ── TTS / STT notes ──────────────────────── */
.tw2-note {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 0.7rem;
}

.tw2-stt-interim {
  min-height: 60px;
  margin-top: 0.8rem;
  font-size: 0.9rem;
  font-style: italic;
  color: var(--primary);
  background: var(--bg-tertiary);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
  padding: 0.6rem 0.9rem;
}

/* ── Toolbar (sample / upload) ────────────── */
.tw2-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.tw2-btn--xs {
  font-size: 0.8rem;
  padding: 0.35rem 0.8rem;
  border-radius: var(--radius-sm);
}

/* ── TEXT TO SPEECH PRO ───────────────────── */
.tts-pro__layout {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 1.25rem;
  align-items: stretch;
}

.tts-pro__editor { display: flex; flex-direction: column; min-width: 0; }
.tts-pro__editor .tw2-input { flex: 1; min-height: 220px; }

.tts-pro__toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.tts-pro__limit {
  font-size: 0.78rem;
  color: var(--text-tertiary);
  margin-left: auto;
}

.tts-pro__player {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  min-width: 0;
}

.tts-pro__sentence {
  min-height: 96px;
  max-height: 150px;
  overflow-y: auto;
  font-size: 1.05rem;
  line-height: 1.9;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 0.75rem;
}

.tts-pro__word {
  border-radius: 4px;
  padding: 0 1px;
  transition: background 0.15s ease, color 0.15s ease;
  cursor: pointer;
}

.tts-pro__word.is-speaking {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: var(--white);
  border-radius: 6px;
  padding: 0 4px;
}

.tts-pro__placeholder,
.tts-pro__done {
  color: var(--text-tertiary);
  font-style: italic;
  font-size: 0.9rem;
}

.tts-pro__progress {
  height: 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.tts-pro__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: var(--radius-full);
  transition: width 0.25s linear;
}

.tts-pro__times {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-tertiary);
  margin-top: -0.6rem;
  font-variant-numeric: tabular-nums;
}

.tts-pro__controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.tts-pro__play {
  flex: 0 0 56px;
  height: 56px;
  border-radius: var(--radius-full);
  border: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  color: var(--white);
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  box-shadow: var(--shadow-md);
  position: relative;
  transition: transform var(--transition), box-shadow var(--transition);
}

.tts-pro__play:hover { box-shadow: var(--shadow-glow); transform: scale(1.04); }
.tts-pro__play .icon { width: 26px; height: 26px; margin-left: 3px; }

/* playing state: swap the play triangle for two pause bars */
.tts-pro__play.is-active svg { display: none; }
.tts-pro__play.is-active::before,
.tts-pro__play.is-active::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 6px;
  height: 22px;
  border-radius: 3px;
  background: var(--white);
  transform: translateY(-50%);
}
.tts-pro__play.is-active::before { left: calc(50% - 9px); }
.tts-pro__play.is-active::after { left: calc(50% + 3px); }

.tts-pro__status {
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-height: 1.2em;
}

.tts-pro__hint {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  margin: 0;
}

.tts-pro__settings {
  display: grid;
  grid-template-columns: minmax(200px, 1.4fr) repeat(3, 1fr);
  gap: 1rem;
  align-items: end;
  margin: 1.1rem 0 0.6rem;
}

.tts-pro__slider label {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.tts-pro__slider label span { color: var(--primary); font-weight: 700; }

.tts-pro__presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.1rem;
}

.tts-pro__presets button {
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 0.4rem 1rem;
  cursor: pointer;
  transition: all var(--transition);
}

.tts-pro__presets button:hover { border-color: var(--primary); color: var(--text-primary); }
.tts-pro__presets button.is-active {
  color: var(--white);
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  border-color: transparent;
  box-shadow: var(--shadow-sm);
}

/* ── Related tools grid ───────────────────── */
.tw2-related {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.9rem;
  margin-top: 1rem;
}

.tw2-related a {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.85rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 600;
  transition: transform var(--transition), border-color var(--transition), box-shadow var(--transition);
}

.tw2-related a:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: var(--shadow-md);
}

.tw2-related__icon {
  flex: 0 0 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: var(--white);
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
}

/* ── TOC + takeaways (article helpers) ────── */
.toc {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
}

.toc ol { margin: 0.6rem 0 0; padding-left: 1.25rem; }
.toc li { margin-bottom: 0.4rem; font-size: 0.92rem; }
.toc li a { color: var(--primary); text-decoration: none; }
.toc li a:hover { text-decoration: underline; }

.key-takeaways {
  background: linear-gradient(135deg, color-mix(in srgb, var(--primary) 6%, transparent), color-mix(in srgb, var(--accent) 6%, transparent));
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
}

.key-takeaways ul { margin: 0.6rem 0 0; padding-left: 1.25rem; }
.key-takeaways li { margin-bottom: 0.45rem; font-size: 0.95rem; }

/* ── Responsive ───────────────────────────── */
@media (max-width: 1024px) {
  .tts-pro__layout { grid-template-columns: 1fr; }
  .tts-pro__settings { grid-template-columns: 1fr 1fr 1fr; }
  .tts-pro__voice { grid-column: span 3; }
}

@media (max-width: 768px) {
  .tw2 { margin-top: -3rem; padding: 0 1rem; }
  .tw2__panel { padding: 1.1rem; }
  .tw2-col { grid-template-columns: 1fr; }
  .tw2-stat--wide { grid-column: span 1; }
  .tw2-opt select,
  .tw2-opt input[type="number"],
  .tw2-opt input[type="text"] { min-width: 100%; width: 100%; }
  .tts-pro__settings { grid-template-columns: 1fr; }
  .tts-pro__voice { grid-column: span 1; }
  .tts-pro__limit { margin-left: 0; width: 100%; }
}

/* ── Light theme ──────────────────────────── */
[data-theme="light"] .tw2__panel {
  box-shadow: var(--shadow-lg);
}

[data-theme="light"] .tw2-input,
[data-theme="light"] .tw2-output {
  background: var(--white);
}

[data-theme="light"] .tw2-diff-row--added { background: color-mix(in srgb, var(--success) 10%, transparent); }
[data-theme="light"] .tw2-diff-row--removed { background: color-mix(in srgb, var(--error) 10%, transparent); }

[data-theme="light"] .tts-pro__player { box-shadow: var(--shadow-sm); }
[data-theme="light"] .tts-pro__sentence { color: var(--text-secondary); }
