:root {
  color-scheme: light;
  --bg: #e6e1d6;
  --toolbar: #f7f4ee;
  --editor: #f3efe6;
  --preview: #ffffff;
  --text: #1d1a16;
  --muted: #746b60;
  --line: #e3dcd0;
  --hover: rgba(29, 26, 22, 0.05);
  --accent: #0f5c4c;
  --on-accent: #f4fffb;
  --accent-soft: rgba(15, 92, 76, 0.1);
  --code-chip: #f3efe6;
  --code-block: #231f1b;
  --code-text: #f6f1e7;
  --quote: #f6f1e6;
  --table-head: #f3eee4;
  --row: rgba(15, 92, 76, 0.035);
  --select: #d5eee4;
  --drop: rgba(255, 253, 251, 0.94);
  --shadow: 0 16px 50px rgba(60, 42, 20, 0.08);
  --sans: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  --mono: "IBM Plex Mono", "Cascadia Mono", "Cascadia Code", Consolas, monospace;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #100f0d;
  --toolbar: #1a1916;
  --editor: #141311;
  --preview: #1c1b18;
  --text: #f4f0e8;
  --muted: #b4aa9e;
  --line: #34302a;
  --hover: rgba(255, 248, 236, 0.06);
  --accent: #e2b07a;
  --on-accent: #23180f;
  --accent-soft: rgba(226, 176, 122, 0.14);
  --code-chip: #2a261f;
  --code-block: #0e0d0c;
  --code-text: #f6f1e7;
  --quote: #2a241c;
  --table-head: #2a261f;
  --row: rgba(255, 248, 236, 0.03);
  --select: #5c4630;
  --drop: rgba(28, 27, 24, 0.94);
  --shadow: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: var(--select);
  color: var(--text);
}

* {
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  min-height: 60px;
  padding: 10px 16px;
  background: var(--toolbar);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-inline-end: 6px;
  white-space: nowrap;
}

.brand h1 {
  margin: 0;
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.2;
}

.noscript {
  margin: 0;
  padding: 14px 16px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

.brand span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.seg {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--editor);
}

.seg button {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  min-width: 64px;
  height: 32px;
  padding: 0 12px;
  cursor: pointer;
}

.seg button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--on-accent);
}

.seg button:hover:not([aria-pressed="true"]) {
  color: var(--text);
}

.btn {
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 9px;
  height: 34px;
  padding: 0 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}

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

.btn:focus-visible,
.seg button:focus-visible,
.check input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn input { display: none; }

.check {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 14px;
  user-select: none;
}

.check input {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--accent);
}

.grow { margin-inline-start: auto; }

main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 16px minmax(0, 1fr);
  padding: 12px;
}

.splitter {
  position: relative;
  touch-action: none;
  cursor: col-resize;
}

.splitter::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 52px;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  background: var(--toolbar);
  border: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(40, 30, 16, 0.12);
}

.splitter:hover::before,
.splitter:focus-visible::before,
body.splitting .splitter::before {
  background: var(--accent);
  border-color: var(--accent);
  height: 72px;
}

.splitter:focus-visible {
  outline: none;
}

body.splitting,
body.splitting * {
  cursor: col-resize !important;
  user-select: none;
}

.pane {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.editor-pane { background: var(--editor); }
.preview-pane { background: var(--preview); }

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 42px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.file-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.editor-head {
  flex-wrap: wrap;
  row-gap: 1px;
  padding-top: 8px;
  padding-bottom: 8px;
}

.editor-head .file-name {
  flex: 1;
  text-align: end;
}

.insert-hint {
  flex: 1 0 100%;
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.4;
}

.insert-hint button {
  font: inherit;
  font-weight: 600;
  color: var(--accent);
  background: transparent;
  border: 0;
  padding: 0;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 0.16em;
}

.insert-hint button:hover { color: var(--text); }

.insert-hint button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.editor-pane:focus-within {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
}

.editor-pane:focus-within .pane-head { color: var(--accent); }
.editor-pane:focus-within .insert-hint { color: var(--muted); }

textarea,
#preview {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

textarea {
  width: 100%;
  margin: 0;
  padding: 28px 26px 48px;
  border: 0;
  resize: none;
  outline: none;
  background: transparent;
  color: inherit;
  font: 16.5px/1.9 var(--sans);
  caret-color: var(--accent);
  tab-size: 2;
  /* Arabic lines open on the right. Fences and commands stay in Latin order. */
  direction: ltr;
  unicode-bidi: plaintext;
  text-align: start;
}

html[dir="ltr"] textarea {
  direction: ltr;
  unicode-bidi: normal;
  text-align: left;
}

textarea::placeholder { color: var(--muted); }

#preview {
  padding: 32px clamp(22px, 4vw, 52px) 64px;
  font-size: 18px;
  font-weight: 400;
  line-height: 1.95;
  text-align: start;
}

#preview.is-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  font-size: 17px;
}

#preview > :first-child { margin-top: 0; }

#preview p,
#preview ul,
#preview ol,
#preview blockquote,
#preview h1,
#preview h2,
#preview h3,
#preview h4,
#preview h5,
#preview h6 {
  max-width: 42rem;
}

#preview h1,
#preview h2,
#preview h3,
#preview h4 {
  line-height: 1.45;
  font-weight: 700;
  margin: 1.35em 0 0.45em;
}

#preview h1 { font-size: 1.85rem; }
#preview h2 {
  font-size: 1.38rem;
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--line);
}
#preview h3 { font-size: 1.16rem; }

#preview p,
#preview ul,
#preview ol,
#preview blockquote,
#preview pre,
#preview table {
  margin: 0.85em 0;
}

#preview ul,
#preview ol {
  padding-inline-start: 1.35em;
}

#preview li::marker { color: var(--accent); }
#preview li + li { margin-top: 0.25em; }

#preview li input[type="checkbox"] {
  margin-inline-end: 0.35em;
  accent-color: var(--accent);
}

#preview a {
  color: var(--accent);
  text-underline-offset: 0.18em;
}

#preview strong { font-weight: 700; }

#preview em {
  font-style: normal;
  font-weight: 600;
  color: var(--accent);
}

#preview blockquote {
  margin-inline: 0;
  padding: 0.35em 1em 0.45em;
  background: var(--quote);
  border-inline-start: 3px solid var(--accent);
  border-start-end-radius: 12px;
  border-end-end-radius: 12px;
}

#preview hr {
  border: 0;
  height: 1px;
  margin: 1.8em 0;
  background: var(--line);
  max-width: 42rem;
}

#preview img {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
}

#preview table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92em;
  line-height: 1.7;
  background: transparent;
}

#preview th,
#preview td {
  border: 1px solid var(--line);
  padding: 10px 14px;
  text-align: start;
  vertical-align: top;
}

#preview th[align],
#preview td[align] {
  text-align: start;
}

#preview thead th {
  background: var(--table-head);
  font-weight: 600;
}

#preview tbody tr:nth-child(even) td { background: var(--row); }

#preview code {
  font-family: var(--mono);
  direction: ltr;
  unicode-bidi: isolate;
  text-align: left;
}

#preview :not(pre) > code {
  background: var(--code-chip);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.08em 0.42em;
  font-size: 0.82em;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

#preview pre {
  direction: ltr;
  unicode-bidi: isolate;
  text-align: left;
  width: 100%;
  background: var(--code-block);
  color: var(--code-text);
  border-radius: 12px;
  padding: 16px 18px;
  overflow: auto;
  line-height: 1.65;
}

#preview .code-block {
  direction: ltr;
  margin: 0.85em 0;
  max-width: 100%;
  border-radius: 12px;
  overflow: hidden;
  background: var(--code-block);
}

#preview .code-block pre {
  margin: 0;
  border: 0;
  border-radius: 0;
}

#preview .code-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 8px 8px 0 12px;
}

#preview .code-lang {
  margin-inline-end: auto;
  color: rgba(246, 241, 231, 0.7);
  font-family: var(--mono);
  font-size: 12px;
}

#preview .code-copy {
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  color: var(--code-text);
  background: transparent;
  border: 1px solid rgba(246, 241, 231, 0.35);
  border-radius: 8px;
  height: 28px;
  padding: 0 10px;
  cursor: pointer;
}

#preview .code-copy:hover {
  background: rgba(246, 241, 231, 0.12);
}

#preview .code-copy:focus-visible {
  outline: 2px solid var(--on-accent);
  outline-offset: 2px;
}

html[data-theme="dark"] #preview pre {
  border: 1px solid var(--line);
}

html[data-theme="dark"] #preview .code-block,
html[data-theme="dark"] #preview .code-block pre {
  border: 0;
}

html[data-theme="dark"] #preview .code-block {
  border: 1px solid var(--line);
}

#preview pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-size: 13.5px;
  color: inherit;
  white-space: pre;
}

#preview td code,
#preview th code { white-space: nowrap; }

.drop {
  display: none;
  position: fixed;
  inset: 14px;
  z-index: 5;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 2px dashed var(--accent);
  border-radius: 18px;
  background: var(--drop);
  color: var(--accent);
  text-align: center;
  padding: 24px;
  pointer-events: none;
}

.drop strong {
  font-size: 1.45rem;
  font-weight: 700;
}

.drop span {
  font-size: 1.05rem;
  font-weight: 500;
}

body.dragging .drop { display: flex; }

@media (max-width: 860px) {
  main {
    grid-template-columns: 1fr !important;
    grid-template-rows: minmax(240px, 1fr) minmax(240px, 1fr);
    padding: 8px;
    row-gap: 8px;
  }

  .splitter { display: none; }
  .brand span { display: none; }
  #preview { font-size: 17px; }
}

@media print {
  body { background: white; color: black; }
  .toolbar, .editor-pane, .pane-head, .insert-hint, .drop, .splitter, .code-bar { display: none !important; }
  main { display: block; padding: 0; }
  .pane { border: 0; box-shadow: none; border-radius: 0; }
  #preview { overflow: visible; background: white; color: black; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
