:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --surface-hover: #eef2ff;
  --text: #172033;
  --text-muted: #64748b;
  --border: #dbe3ef;
  --border-strong: #c5d0df;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: #fee2e2;
  --success: #15803d;
  --success-soft: #dcfce7;
  --shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --editor-font: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  --ui-font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1120;
  --surface: #111827;
  --surface-soft: #172033;
  --surface-hover: #1e293b;
  --text: #e5edf8;
  --text-muted: #94a3b8;
  --border: #273449;
  --border-strong: #3a4962;
  --primary: #60a5fa;
  --primary-hover: #93c5fd;
  --primary-soft: #172f52;
  --danger: #f87171;
  --danger-hover: #fca5a5;
  --danger-soft: #4a1d25;
  --success: #4ade80;
  --success-soft: #123b27;
  --shadow: 0 22px 55px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--ui-font);
  line-height: 1.5;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
[tabindex="0"]:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--primary) 32%, transparent);
  outline-offset: 2px;
}

.skip-link {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1000;
  transform: translateY(-160%);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: #fff;
  text-decoration: none;
  transition: transform .2s ease;
}
.skip-link:focus { transform: translateY(0); }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 76px;
  padding: 12px clamp(18px, 4vw, 52px);
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(16px);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 13px;
  background: linear-gradient(145deg, var(--primary), #7c3aed);
  color: #fff;
  font-weight: 900;
  letter-spacing: -1px;
  box-shadow: 0 9px 22px color-mix(in srgb, var(--primary) 28%, transparent);
}
.brand h1 { margin: 0; font-size: 1.12rem; line-height: 1.2; }
.brand p { margin: 2px 0 0; color: var(--text-muted); font-size: .82rem; }

.header-actions { display: flex; align-items: center; gap: 12px; }
.save-status { color: var(--text-muted); font-size: .82rem; }
.save-status.is-saving { color: var(--primary); }
.save-status.is-saved { color: var(--success); }

.icon-button,
.tool-button {
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  transition: background .18s ease, border-color .18s ease, transform .18s ease, color .18s ease;
}
.icon-button:hover,
.tool-button:hover { background: var(--surface-hover); border-color: var(--border-strong); color: var(--primary); }
.icon-button:active,
.tool-button:active { transform: translateY(1px); }
.icon-button { width: 42px; height: 42px; border-radius: 12px; font-size: 1.2rem; }

.app-shell { width: min(1540px, calc(100% - 32px)); margin: 24px auto 34px; }

.action-bar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--surface);
}

.document-name-group label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-muted);
  font-size: .78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
}
.filename-input {
  display: flex;
  align-items: center;
  min-width: min(320px, 100%);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-soft);
  overflow: hidden;
}
.filename-input:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 16%, transparent); }
.filename-input input {
  width: 100%;
  min-width: 0;
  padding: 10px 2px 10px 12px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-weight: 650;
}
.filename-input span { padding: 10px 12px 10px 4px; color: var(--text-muted); }

.action-buttons { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 42px;
  padding: 9px 14px;
  border: 1px solid transparent;
  border-radius: 10px;
  font-weight: 750;
  transition: transform .18s ease, background .18s ease, border-color .18s ease;
}
.button:active { transform: translateY(1px); }
.button-primary { background: var(--primary); color: #fff; }
.button-primary:hover { background: var(--primary-hover); }
.button-secondary { border-color: var(--border); background: var(--surface); color: var(--text); }
.button-secondary:hover { background: var(--surface-hover); border-color: var(--border-strong); }
.button-danger-ghost { background: transparent; color: var(--danger); }
.button-danger-ghost:hover { background: var(--danger-soft); }
.button-danger { background: var(--danger); color: #fff; }
.button-danger:hover { background: var(--danger-hover); }

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-top: 0;
  background: var(--surface-soft);
  overflow-x: auto;
  scrollbar-width: thin;
}
.toolbar-group { display: flex; gap: 5px; }
.toolbar-divider { flex: 0 0 1px; width: 1px; height: 26px; margin: 0 2px; background: var(--border); }
.tool-button { flex: 0 0 auto; width: 36px; height: 34px; border-radius: 8px; font-size: .9rem; }
.tool-button.text-tool { width: auto; min-width: 38px; padding-inline: 8px; font-size: .76rem; font-weight: 800; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  min-height: clamp(540px, 72vh, 860px);
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel { display: flex; min-width: 0; flex-direction: column; }
.editor-panel { border-right: 1px solid var(--border); }
.panel-header {
  display: flex;
  min-height: 67px;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.panel-header h2 { margin: 0; font-size: .97rem; }
.panel-header p { margin: 2px 0 0; color: var(--text-muted); font-size: .74rem; }

.stats { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; color: var(--text-muted); font-size: .72rem; }
.stats strong { color: var(--text); }
.live-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 9px;
  border-radius: 999px;
  background: var(--success-soft);
  color: var(--success);
  font-size: .72rem;
  font-weight: 800;
}
.live-badge > span { width: 7px; height: 7px; border-radius: 50%; background: currentColor; box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 14%, transparent); }

#editor {
  flex: 1;
  width: 100%;
  min-height: 470px;
  resize: none;
  padding: 24px;
  border: 0;
  outline: 0;
  background: var(--surface-soft);
  color: var(--text);
  font-family: var(--editor-font);
  font-size: .95rem;
  line-height: 1.72;
  tab-size: 2;
}
#editor::selection { background: color-mix(in srgb, var(--primary) 24%, transparent); }

.markdown-body {
  flex: 1;
  min-height: 470px;
  padding: 24px clamp(22px, 3vw, 42px) 48px;
  overflow: auto;
  background: var(--surface);
  color: var(--text);
  overflow-wrap: anywhere;
}
.markdown-body > :first-child { margin-top: 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 {
  margin: 1.55em 0 .7em;
  line-height: 1.25;
  color: var(--text);
}
.markdown-body h1 { padding-bottom: .35em; border-bottom: 1px solid var(--border); font-size: 2rem; }
.markdown-body h2 { padding-bottom: .3em; border-bottom: 1px solid var(--border); font-size: 1.5rem; }
.markdown-body h3 { font-size: 1.22rem; }
.markdown-body p, .markdown-body ul, .markdown-body ol { margin: .9em 0; }
.markdown-body a { color: var(--primary); text-decoration-thickness: 1.5px; text-underline-offset: 3px; }
.markdown-body img { display: block; max-width: 100%; height: auto; margin: 1.25rem auto; border-radius: 10px; }
.markdown-body blockquote {
  margin: 1.2em 0;
  padding: .2em 1em;
  border-left: 4px solid var(--primary);
  background: var(--primary-soft);
  color: var(--text-muted);
}
.markdown-body blockquote > :first-child { margin-top: .6em; }
.markdown-body blockquote > :last-child { margin-bottom: .6em; }
.markdown-body code {
  padding: .18em .38em;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-soft);
  font-family: var(--editor-font);
  font-size: .88em;
}
.markdown-body pre {
  margin: 1.25em 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0f172a;
  color: #e2e8f0;
  overflow: auto;
}
.markdown-body pre code { padding: 0; border: 0; background: transparent; color: inherit; }
.markdown-body hr { margin: 2em 0; border: 0; border-top: 1px solid var(--border); }
.markdown-body table { display: block; width: 100%; border-spacing: 0; border-collapse: collapse; overflow: auto; }
.markdown-body th, .markdown-body td { padding: 9px 12px; border: 1px solid var(--border); text-align: left; }
.markdown-body th { background: var(--surface-soft); }
.markdown-body input[type="checkbox"] { margin-right: .5em; accent-color: var(--primary); }
.markdown-body .empty-preview { display: grid; min-height: 360px; place-items: center; text-align: center; color: var(--text-muted); }
.markdown-body .empty-preview div { max-width: 310px; }
.markdown-body .empty-preview strong { display: block; margin-bottom: 6px; color: var(--text); font-size: 1.05rem; }

.app-footer {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-top: 18px;
  padding: 22px 4px 6px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: .82rem;
}
.footer-brand,
.footer-developer { display: grid; gap: 5px; }
.footer-brand strong {
  color: var(--text);
  font-size: .95rem;
  font-weight: 700;
}
.footer-developer { justify-items: end; text-align: right; }
.footer-developer > span {
  color: var(--text);
  font-weight: 600;
}
.footer-links {
  display: flex;
  align-items: center;
  gap: 8px;
}
.footer-links a {
  color: var(--primary);
  font-weight: 700;
  text-decoration: none;
}
.footer-links a:hover,
.footer-links a:focus-visible { text-decoration: underline; }

.toast-container { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: grid; gap: 10px; width: min(360px, calc(100vw - 36px)); }
.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 13px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  animation: toast-in .25s ease both;
}
.toast.is-leaving { animation: toast-out .22s ease both; }
.toast-icon { display: grid; place-items: center; flex: 0 0 22px; width: 22px; height: 22px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); font-weight: 900; }
.toast.success .toast-icon { background: var(--success-soft); color: var(--success); }
.toast.error .toast-icon { background: var(--danger-soft); color: var(--danger); }
.toast p { margin: 0; font-size: .86rem; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

.confirm-dialog {
  width: min(430px, calc(100% - 28px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 28px 80px rgba(0,0,0,.28);
}
.confirm-dialog::backdrop { background: rgba(2, 6, 23, .62); backdrop-filter: blur(3px); }
.confirm-dialog form { padding: 26px; text-align: center; }
.dialog-icon { display: grid; place-items: center; width: 48px; height: 48px; margin: 0 auto 14px; border-radius: 50%; background: var(--danger-soft); color: var(--danger); font-size: 1.3rem; font-weight: 900; }
.confirm-dialog h2 { margin: 0; font-size: 1.2rem; }
.confirm-dialog p { margin: 9px 0 22px; color: var(--text-muted); }
.dialog-actions { display: flex; justify-content: center; gap: 9px; }

.sr-only { position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip: rect(0,0,0,0) !important; white-space: nowrap !important; border: 0 !important; }
.noscript-message { position: fixed; inset: auto 16px 16px; z-index: 200; padding: 14px; border-radius: 12px; background: var(--danger); color: #fff; text-align: center; }

@media (max-width: 920px) {
  .action-bar { align-items: stretch; flex-direction: column; }
  .action-buttons { justify-content: flex-start; }
  .workspace { grid-template-columns: 1fr; }
  .editor-panel { border-right: 0; border-bottom: 1px solid var(--border); }
  #editor, .markdown-body { min-height: 430px; }
  .app-footer { align-items: flex-start; flex-direction: column; }
  .footer-developer { justify-items: start; text-align: left; }
}

@media (max-width: 620px) {
  .app-header { padding-inline: 16px; }
  .brand p, .save-status { display: none; }
  .app-shell { width: min(100% - 20px, 1540px); margin-top: 10px; }
  .action-bar { padding: 13px; }
  .document-name-group, .filename-input { width: 100%; }
  .action-buttons { display: grid; grid-template-columns: 1fr 1fr; }
  .button { width: 100%; }
  .panel-header { align-items: flex-start; flex-direction: column; gap: 8px; }
  .stats { justify-content: flex-start; }
  #editor, .markdown-body { min-height: 390px; padding: 18px; }
  .dialog-actions { flex-direction: column-reverse; }
}

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