/* Character Counter Notepad — a quiet notepad. */

:root {
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: ui-serif, "New York", "Iowan Old Style", "Charter", Georgia, serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;

  --bg: #ffffff;
  --surface: #ffffff;
  --text: #1d1d1f;
  --text-2: #6e6e73;
  --text-3: #a1a1a6;
  --hair: rgba(0, 0, 0, 0.1);
  --fill: rgba(0, 0, 0, 0.045);
  --fill-2: rgba(0, 0, 0, 0.08);
  --mark: rgba(255, 204, 0, 0.34);
  --mark-cur: rgba(255, 159, 10, 0.6);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.08), 0 0 0 0.5px rgba(0, 0, 0, 0.1);

  --bar-h: 52px;
  --foot-h: 44px;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #0f0f10;
  --surface: #1c1c1e;
  --text: #f5f5f7;
  --text-2: #a1a1a6;
  --text-3: #636366;
  --hair: rgba(255, 255, 255, 0.12);
  --fill: rgba(255, 255, 255, 0.06);
  --fill-2: rgba(255, 255, 255, 0.11);
  --mark: rgba(255, 214, 10, 0.24);
  --mark-cur: rgba(255, 159, 10, 0.55);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.5), 0 0 0 0.5px rgba(255, 255, 255, 0.1);
  color-scheme: dark;
}

/* Zen: warmer paper, softer ink. */
html.zen[data-theme="light"] { --bg: #f7f6f2; --surface: #fdfcfa; --text: #2b2a28; --text-2: #75736e; }
html.zen[data-theme="dark"]  { --bg: #121212; --surface: #1c1c1c; --text: #d9d8d4; --text-2: #8e8d89; }

* { box-sizing: border-box; }

html {
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  transition: background-color 0.4s var(--ease), color 0.4s var(--ease);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font: 400 17px/1.65 var(--sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible, textarea:focus-visible { outline: none; }
button:focus-visible { box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px var(--text-2); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Bars ---------- */

.bar, .foot {
  position: sticky;
  z-index: 10;
  display: flex;
  align-items: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  transition: opacity 0.5s var(--ease), background-color 0.4s var(--ease), border-color 0.4s var(--ease);
}

.bar {
  top: 0;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(24px, env(safe-area-inset-left));
  justify-content: space-between;
  border-bottom: 0.5px solid var(--hair);
}

.brand { min-width: 0; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions { flex: none; }
@media (max-width: 420px) { .brand-tail { display: none; } }

.actions { display: flex; gap: 0; }

.icon {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 10px;
  color: var(--text-2);
  transition: color 0.2s, background-color 0.2s;
}
.icon:hover { color: var(--text); background: var(--fill); }
.icon:active { background: var(--fill-2); }
.icon[aria-pressed="true"] { color: var(--text); }

.i-done, .i-sun { display: none; }
.icon.done .i-copy { display: none; }
.icon.done .i-done { display: block; }
[data-theme="dark"] .i-moon { display: none; }
[data-theme="dark"] .i-sun { display: block; }

.foot {
  bottom: 0;
  min-height: calc(var(--foot-h) + env(safe-area-inset-bottom));
  padding: 0 max(24px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(24px, env(safe-area-inset-left));
  gap: 24px;
  justify-content: space-between;
  border-top: 0.5px solid var(--hair);
  font-size: 13px;
  line-height: 1.3;
  color: var(--text-2);
}

.stats { display: flex; gap: 24px; margin: 0; }
.stat { display: flex; gap: 5px; align-items: baseline; white-space: nowrap; }
.stat dd { margin: 0; color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; }
.stat dt { margin: 0; }

.sel { display: flex; gap: 12px; white-space: nowrap; }
.sel[hidden] { display: none; }
.sel b { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; }

/* ---------- Editor ---------- */

main {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 64px max(24px, env(safe-area-inset-right)) 25vh max(24px, env(safe-area-inset-left));
  transition: padding 0.5s var(--ease);
}

.editor {
  position: relative;
  width: 100%;
  max-width: 700px;
  transition: max-width 0.5s var(--ease);
}

/* The mirror sits in flow, sizes the editor, and paints match highlights behind the textarea. */
.mirror, .ta {
  margin: 0; padding: 0; border: 0;
  font: inherit;
  letter-spacing: inherit;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  tab-size: 4;
}

.mirror {
  min-height: 50vh;
  color: transparent;
  pointer-events: none;
  user-select: none;
}
.mirror mark {
  color: transparent;
  background: var(--mark);
  border-radius: 2px;
  box-shadow: 0 0 0 1px var(--mark);
}
.mirror mark.cur { background: var(--mark-cur); box-shadow: 0 0 0 1px var(--mark-cur); }

.ta {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: var(--text);
  caret-color: var(--text);
  outline: none;
  transition: color 0.4s var(--ease);
}
.ta::placeholder { color: var(--text-3); opacity: 1; }

/* ---------- Find & replace ---------- */

.find {
  position: fixed;
  z-index: 20;
  top: calc(var(--bar-h) + env(safe-area-inset-top) + 12px);
  right: max(16px, env(safe-area-inset-right));
  width: 392px;
  max-width: calc(100vw - 32px);
  padding: 10px;
  display: grid;
  gap: 6px;
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow);
  animation: drop 0.22s var(--ease);
}
.find[hidden] { display: none; }
@keyframes drop { from { opacity: 0; transform: translateY(-6px); } }

.row { display: flex; gap: 6px; align-items: flex-start; }

.find textarea {
  flex: 1;
  min-width: 0;
  height: 34px;
  max-height: 168px;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: var(--fill);
  color: var(--text);
  font: 13px/18px var(--mono);
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  transition: box-shadow 0.15s;
}
.find textarea::placeholder { color: var(--text-3); font-family: var(--sans); opacity: 1; }
.find textarea:focus { box-shadow: inset 0 0 0 1px var(--text-3); }
.find textarea.invalid { box-shadow: inset 0 0 0 1px #d70015; }
[data-theme="dark"] .find textarea.invalid { box-shadow: inset 0 0 0 1px #ff6961; }

.opts { display: flex; gap: 2px; }

.opt {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 8px;
  color: var(--text-2);
  font: 500 13px/1 var(--sans);
  transition: color 0.15s, background-color 0.15s;
}
.opt svg { width: 18px; height: 18px; }
.opt:hover { color: var(--text); background: var(--fill); }
.opt[aria-pressed="true"] { color: var(--bg); background: var(--text); }
#optRe { font-family: var(--mono); letter-spacing: -0.04em; }
.ww { text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px; }

.meta { display: flex; align-items: center; gap: 10px; white-space: nowrap; padding: 2px 0 0 4px; min-height: 34px; font-size: 12px; color: var(--text-2); }
.count { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 500; }
.count:empty { display: none; }
.hint { color: var(--text-2); }
.spacer { flex: 1; }

.text-btn {
  flex: none;
  height: 30px;
  padding: 0 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--fill);
  transition: background-color 0.15s, opacity 0.15s;
}
.text-btn:hover { background: var(--fill-2); }
.text-btn:disabled { opacity: 0.4; cursor: default; background: var(--fill); }

/* ---------- Zen ---------- */

html.zen .bar, html.zen .foot {
  background: transparent;
  border-color: transparent;
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
}
html.zen .brand, html.zen #findBtn, html.zen #copyBtn { opacity: 0; pointer-events: none; }
html.zen .foot { justify-content: center; }
html.zen .stat:not(.words), html.zen .sel { display: none; }
html.zen.typing .bar, html.zen.typing .foot { opacity: 0; pointer-events: none; }
html.zen .find { top: calc(env(safe-area-inset-top) + 16px); }

html.zen main { padding-top: 16vh; }
html.zen .editor {
  max-width: 620px;
  font-family: var(--serif);
  font-size: 20px;
  line-height: 1.8;
}

/* ---------- Small screens & touch ---------- */

@media (max-width: 640px) {
  body { font-size: 16px; }
  main { padding-top: 32px; }
  .stats { gap: 16px; }
  .stat.extra { display: none; }
  .foot { gap: 16px; }
  .find { left: 8px; right: 8px; width: auto; max-width: none; top: calc(var(--bar-h) + env(safe-area-inset-top) + 8px); }
  .hint { display: none; }
  html.zen .editor { font-size: 18px; }
  html.zen main { padding-top: 10vh; }
}

@media (pointer: coarse) {
  .opt { width: 40px; height: 40px; }
  .find textarea { height: 40px; padding: 11px 10px; }
  .text-btn { height: 40px; }
}

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