*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #ffffff;
  --surface: #f7f7f7;
  --border: #e8e8e8;
  --border-dim: #efefef;
  --text: #1a1a1a;
  --text-muted: #767676;
  --text-dim: #ccc;
  --act-hover: rgba(0,0,0,0.05);
  --act-active: rgba(0,0,0,0.09);
  --caret: #2563eb;
  --select: rgba(37,99,235,0.14);
  --font-mono: "Cascadia Code", "JetBrains Mono", "Fira Code", ui-monospace, monospace;
  --font-ui: "Inter", system-ui, -apple-system, sans-serif;
  --toolbar-h: 44px;
  --gutter-w: 52px;
  --line-h: 1.65rem;
  --ease: 140ms ease;
}

html {
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.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;
}

.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  background: var(--text);
  color: var(--bg);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 0 0 6px 0;
  text-decoration: none;
  z-index: 999;
  transition: top 100ms ease;
}

.skip-link:focus {
  top: 0;
}

.hidden {
  display: none !important;
}

#toolbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  height: var(--toolbar-h);
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 8px;
  z-index: 10;
  user-select: none;
  -webkit-user-select: none;
}

#logo {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  opacity: 0.9;
  transition: opacity var(--ease);
  flex-shrink: 0;
}

#logo:hover { opacity: 0.55; }

#logo:focus-visible {
  outline: 1.5px solid var(--caret);
  outline-offset: 3px;
  border-radius: 3px;
}

#actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 2px;
}

.act {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background var(--ease), color var(--ease);
  flex-shrink: 0;
  position: relative;
}

.act:hover {
  background: var(--act-hover);
  color: var(--text);
}

.act:active {
  background: var(--act-active);
}

.act:focus-visible {
  outline: 1.5px solid var(--caret);
  outline-offset: 1px;
}

.act:disabled,
.act[aria-disabled="true"] {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

.act.active-mode {
  color: var(--caret);
}

#content {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

#gutter {
  width: var(--gutter-w);
  flex-shrink: 0;
  padding: 14px 10px 14px 0;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: var(--line-h);
  color: var(--text-dim);
  background: var(--surface);
  border-right: 1px solid var(--border-dim);
  overflow: hidden;
  user-select: none;
  transition: border-color var(--ease);
}

#gutter span {
  display: block;
  transition: color 80ms ease;
}

#gutter span.current {
  color: var(--text-muted);
}

#editor {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font-mono);
  font-size: 0.855rem;
  line-height: var(--line-h);
  padding: 14px 20px;
  tab-size: 4;
  caret-color: var(--caret);
  overflow: auto;
  white-space: pre;
  font-feature-settings: "liga" 1, "calt" 1;
  text-rendering: optimizeLegibility;
  transition: opacity var(--ease);
}

#editor::placeholder {
  color: var(--text-dim);
}

#editor::selection {
  background: var(--select);
}

#editor:focus {
  caret-color: var(--caret);
}

body.typing #gutter {
  border-right-color: #c7d7f5;
}

@keyframes caret-breathe {
  0%, 100% { caret-color: #2563eb; }
  50% { caret-color: #3b82f6; }
}

#editor:focus {
  animation: caret-breathe 2s ease-in-out infinite;
}

#viewer {
  flex: 1;
  background: var(--bg);
  font-family: var(--font-mono);
  font-size: 0.855rem;
  line-height: var(--line-h);
  padding: 14px 20px;
  overflow: auto;
  white-space: pre;
  border: none;
  outline: none;
  font-feature-settings: "liga" 1, "calt" 1;
}

#viewer:focus-visible {
  outline: 1px solid var(--border);
  outline-offset: -1px;
}

#viewer code {
  background: none !important;
  padding: 0 !important;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
}

#raw-view {
  flex: 1;
  background: #fafafa;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.855rem;
  line-height: var(--line-h);
  padding: 14px 20px;
  overflow: auto;
  white-space: pre;
  border: none;
  outline: none;
}

#raw-view:focus-visible {
  outline: 1px solid var(--border);
  outline-offset: -1px;
}

#toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  background: #1a1a1a;
  color: #f0f0f0;
  border: 1px solid #333;
  font-size: 0.78rem;
  letter-spacing: 0.01em;
  padding: 7px 14px;
  border-radius: 6px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease, transform 150ms ease;
  white-space: nowrap;
  z-index: 99;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.hljs {
  background: var(--bg) !important;
  color: var(--text) !important;
}

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

@media (max-width: 540px) {
  :root {
    --gutter-w: 38px;
    --toolbar-h: 40px;
  }

  #logo span { display: none; }
  #editor, #viewer, #raw-view { padding: 12px; }
}
