/* ---------- Theme tokens ---------- */
:root {
  color-scheme: light;
  --bg: #fcfcfc;
  --surface: #f4f4f5;
  --text: #18181b;
  --muted: #71717a;
  --accent: #18181b;
  --link: #18181b;
  --border: #dcdcdc;
  /* Atmita monochrome text.mutedPrimary — button labels/icons. */
  --muted-primary: #1f1f1f;
}

/* Follow the system setting unless the user has picked a theme manually */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #000000;
    --surface: #1c1c1c;
    --text: #fafafa;
    --muted: #a1a1aa;
    --accent: #fafafa;
    --link: #fafafa;
    --border: #1e1e1e;
    --muted-primary: rgba(255, 255, 255, 0.88);
  }
}

/* Manual override (wins over the system preference above) */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #000000;
  --surface: #1c1c1c;
  --text: #fafafa;
  --muted: #a1a1aa;
  --accent: #fafafa;
  --link: #fafafa;
  --border: #1e1e1e;
  --muted-primary: rgba(255, 255, 255, 0.88);
}

/* ---------- Base ---------- */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", "Hiragino Maru Gothic ProN",
    "Quicksand", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(1.25rem, 4vw, 3rem);
  transition: background-color 0.3s ease, color 0.3s ease;
}

/* ---------- App layout ---------- */
.app {
  width: 100%;
  max-width: 780px;
  display: flex;
  flex-direction: column;
  gap: clamp(1.5rem, 4vw, 2.25rem);
  /* Exact vertical center, raised by the footer's height (~1rem: one line
     of 0.75rem text) plus a 2px nudge so the fixed footer below doesn't
     make it feel low. */
  transform: translateY(calc(-1rem - 2px));
}

/* ---------- Stats bar ---------- */
/* Sits above the box; left edge lines up with the text inside the box
   (same inset as the textarea's padding). Height is reserved so the
   layout doesn't shift when counts appear/disappear. */
.stats {
  padding-left: clamp(1.1rem, 3vw, 1.6rem);
  margin-bottom: 0.85rem;
  min-height: 1.6rem;
  white-space: nowrap;
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  font-size: 1rem;
  font-weight: 400;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.stats__value {
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* ---------- Text canvas ---------- */
.input {
  display: block;
  width: 100%;
  /* Fixed height — the box never grows; long text scrolls inside.
     Holds 380px until the viewport can't fit the box plus the surrounding
     chrome (~260px of toolbar/stats/padding), then shrinks. */
  height: clamp(240px, 100dvh - 260px, 380px);
  resize: none;
  overflow-y: auto;
  /* Uniform inset; the input handler scrolls fully down when typing at the
     end so the bottom padding actually shows under the caret. */
  padding: clamp(1.1rem, 3vw, 1.6rem);
  font: inherit;
  font-weight: 400;
  font-size: 16px;
  line-height: 1.6;
  /* Avoid expensive kerning/ligature shaping across very large documents. */
  text-rendering: optimizeSpeed;
  font-kerning: none;
  font-variant-ligatures: none;
  color: var(--text);
  background: transparent;
  border: 2px solid var(--border);
  border-radius: 28px;
  outline: none;
  transition: border-color 0.2s ease, color 0.3s ease;
}

.input::placeholder {
  color: var(--muted);
  opacity: 0.45;
}

/* ---------- Paste zone (empty state) ---------- */
.box {
  position: relative;
}

/* The whole empty box is the paste target; it overlays the textarea. */
.paste-zone {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  border-radius: 28px;
  cursor: pointer;
}

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

.paste-zone--hidden {
  display: none;
}

.paste-zone__hint {
  display: inline-flex;
  align-items: center;
  gap: 0.7rem;
  font-weight: 400;
  font-size: 1.15rem;
  color: var(--muted);
  transition: color 0.2s ease;
}

/* Hovering (or keyboard-focusing) the box lightens the Paste hint. */
.paste-zone:hover .paste-zone__hint,
.paste-zone:focus-visible .paste-zone__hint {
  color: var(--text);
}

.paste-zone__icon {
  width: 22px;
  height: 22px;
}

/* ---------- Markdown preview ---------- */
/* Sits in the same box, replacing the textarea when preview mode is on. */
.preview {
  width: 100%;
  height: clamp(240px, 100dvh - 260px, 380px);
  padding: clamp(1.1rem, 3vw, 1.6rem);
  /* Matches the textarea so the text doesn't resize when toggling preview. */
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: transparent;
  border: 2px solid var(--border);
  border-radius: 28px;
  overflow-wrap: break-word;
  overflow-y: auto;
}

.preview--hidden {
  display: none;
}

.preview > :first-child {
  margin-top: 0;
}
.preview > :last-child {
  margin-bottom: 0;
}

.preview h1,
.preview h2,
.preview h3,
.preview h4,
.preview h5,
.preview h6 {
  margin: 1.4em 0 0.55em;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.02em;
}
.preview h1 {
  font-size: 1.7em;
}
.preview h2 {
  font-size: 1.4em;
}
.preview h3 {
  font-size: 1.2em;
}
.preview h4,
.preview h5,
.preview h6 {
  font-size: 1.05em;
}

.preview p {
  margin: 0.8em 0;
}

.preview ul,
.preview ol {
  margin: 0.8em 0;
  padding-left: 1.4em;
}
.preview li {
  margin: 0.3em 0;
}

.preview a {
  color: var(--link);
  text-decoration: underline;
  text-underline-offset: 0.2em;
}
.preview a:hover {
  text-decoration: none;
}

.preview strong {
  font-weight: 700;
}

.preview code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: color-mix(in srgb, var(--border) 45%, transparent);
  padding: 0.15em 0.4em;
  border-radius: 7px;
}

.preview pre {
  margin: 0.9em 0;
  padding: 1rem 1.15rem;
  background: color-mix(in srgb, var(--border) 40%, transparent);
  border-radius: 16px;
  overflow-x: auto;
}
.preview pre code {
  background: none;
  padding: 0;
}

.preview blockquote {
  margin: 0.9em 0;
  padding: 0.2em 0 0.2em 1em;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

/* Wide tables scroll inside this wrapper instead of overflowing the box. */
.preview .table-wrap {
  margin: 0.9em 0;
  overflow-x: auto;
}
.preview table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.95em;
}
.preview th,
.preview td {
  padding: 0.45em 0.8em;
  border: 2px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
.preview th {
  font-weight: 600;
  background: color-mix(in srgb, var(--border) 30%, transparent);
}

.preview hr {
  border: none;
  border-top: 2px solid var(--border);
  margin: 1.4em 0;
}

/* ---------- Action toolbar (its own row under the box, left-aligned) ---------- */
.editor {
  display: flex;
  flex-direction: column;
}

/* Sits under the box, flush with the container's left edge. Height is
   reserved so the box doesn't shift when the buttons appear/disappear. */
.box-tools {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.4rem;
  margin-top: 1rem;
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  height: 2.4rem;
  padding: 0 0.9rem 0 0.75rem;
  /* Thinner than the big box's 2px — at pill scale 2px reads heavy. */
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted-primary);
  font: inherit;
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  flex-shrink: 0;
  cursor: pointer;
}

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

.pill-btn__icon {
  width: 17px;
  height: 17px;
  display: block;
}
.pill-btn__icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* The X glyph fills its (cropped) box edge-to-edge, so it renders a touch
   smaller than the padded icons to sit at the same optical size. */
.clear-btn .pill-btn__icon {
  width: 11px;
  height: 11px;
}

.preview-btn--hidden,
.replace-btn--hidden,
.clear-btn--hidden {
  display: none;
}

/* ---------- Top-right controls (filter + theme toggle) ---------- */
.top-controls {
  position: fixed;
  /* Above the app content — its translateY creates a stacking context that
     would otherwise paint the toolbar pills over the open filter menu. */
  z-index: 10;
  top: 0.65rem;
  right: 0.65rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.round-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted-primary);
  cursor: pointer;
}

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

.round-btn__icon {
  width: 18px;
  height: 18px;
  display: block;
}

.round-btn__icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* ---------- Stats filter dropdown ---------- */
.stats-filter {
  position: relative;
}

/* Styled after the Atmita context menu: elevated shadowed surface (no
   border), rows with a leading checkbox. */
.filter-menu {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  min-width: 12.5rem;
  padding: 0.625rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -4px rgba(0, 0, 0, 0.1);
}

.filter-menu--hidden {
  display: none;
}

/* Rows skip the hover highlight — the checkbox is the affordance. */
.filter-menu__item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.625rem;
  border-radius: 8px;
  font-size: 0.875rem;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.filter-menu__label {
  flex: 1;
  min-width: 0;
}

/* The real checkbox stays for semantics/keyboard; the box below renders it. */
.filter-menu__item input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  margin: 0;
  pointer-events: none;
}

/* Bare glyph, native-menu style, trailing the label: a check on selected
   rows, a muted dash on unselected ones. Both occupy the same grid cell. */
.filter-menu__check {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

.filter-menu__check svg {
  grid-area: 1 / 1;
  width: 17px;
  height: 17px;
  display: block;
}

.filter-menu__check-on {
  color: var(--text);
  opacity: 0;
}

.filter-menu__check-off {
  color: var(--muted);
  opacity: 1;
}

.filter-menu__item input:checked ~ .filter-menu__check .filter-menu__check-on {
  opacity: 1;
}

.filter-menu__item input:checked ~ .filter-menu__check .filter-menu__check-off {
  opacity: 0;
}

.filter-menu__item input:focus-visible ~ .filter-menu__check {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Footer ---------- */
.footer {
  /* Out of the flow so it doesn't pull the editor off true vertical center. */
  position: fixed;
  /* Original 1.1rem raised by exactly 12px. */
  bottom: calc(1.1rem + 12px);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.75rem;
  color: color-mix(in srgb, var(--muted) 72%, transparent);
  transition: color 0.2s ease;
}

/* The whole line wakes up a little when you're actually over it. */
.footer:hover {
  color: var(--muted);
}

.footer__link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: inherit;
  text-decoration: none;
  transition: color 0.2s ease;
}

.footer__link:hover,
.footer__link:focus-visible {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

.footer__link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
