:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --border: #e8e8ed;
  --border-light: #f0f0f5;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --accent: #0071e3;
  --danger: #b91c1c;
  --radius-sm: 8px;
  --radius-md: 8px;
  --radius-lg: 999px;
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  min-height: 100vh;
  min-height: 100dvh;
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--text);
  letter-spacing: 0;
}

.app {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  transition: background 0.15s;
}

[hidden] {
  display: none !important;
}

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

.noscript-message {
  position: fixed;
  inset: 12px 12px auto;
  z-index: 10;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--danger);
  text-align: center;
}

/* ── Navbar ── */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px max(28px, env(safe-area-inset-right)) 14px max(28px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 12px;
}

.logo {
  font-weight: 600;
  font-size: 1.1rem;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ── File section ── */
.file-section {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
}

input[type="file"] { display: none; }

.file-btn {
  border: none;
  padding: 9px 20px;
  border-radius: var(--radius-lg);
  font-weight: 500;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  white-space: nowrap;
  min-height: 38px;
}

.file-btn:focus-visible,
.view-btn:focus-visible,
.md-selector select:focus-visible,
.panel:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  background: #2c2c2e;
  color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.btn-primary:hover { background: #3a3a3c; transform: scale(1.02); }

.btn-outline {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-outline:hover { background: var(--bg); }

.file-badge {
  font-size: 0.85rem;
  color: var(--text-secondary);
  background: var(--bg);
  padding: 6px 14px;
  border-radius: var(--radius-lg);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 240px;
  overflow: hidden;
  white-space: nowrap;
  min-width: 0;
}

.file-badge span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.md-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border-radius: var(--radius-lg);
  padding: 4px 8px 4px 14px;
  max-width: min(360px, 100%);
  min-width: 0;
}

.md-selector select {
  border: none;
  background: transparent;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
  padding: 5px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  outline: none;
  max-width: 280px;
  min-width: 0;
}

/* ── View control ── */
.view-control {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius-lg);
  padding: 4px;
  gap: 2px;
  flex-shrink: 0;
}

.view-btn {
  padding: 7px 18px;
  border: none;
  background: transparent;
  border-radius: var(--radius-lg);
  font-weight: 500;
  font-size: 0.85rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  white-space: nowrap;
}

.view-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  font-weight: 550;
}

/* ── Main layout ── */
.main-content {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
  min-width: 0;
}

.panel {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px;
  background: var(--surface);
  min-width: 0;
}

.panel.hidden { display: none; }

.split-view #rawPanel { border-right: 1px solid var(--border); }

/* ── Raw panel ── */
.raw-content {
  font-family: 'SF Mono', 'JetBrains Mono', 'Fira Code', monospace;
  background: #fafafa;
  color: var(--text);
  padding: 24px;
  border-radius: var(--radius-md);
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.7;
  font-size: 0.9rem;
  border: 1px solid var(--border-light);
  min-height: 100%;
}

/* ── Preview panel ── */
.preview-content {
  color: var(--text);
  line-height: 1.75;
  font-size: 1rem;
  word-break: break-word;
  overflow-wrap: anywhere;
  max-width: 860px;
  margin: 0 auto;
}

.preview-content h1,
.preview-content h2,
.preview-content h3 {
  font-weight: 600;
  color: var(--text);
  margin-top: 1.6em;
  margin-bottom: 0.5em;
  letter-spacing: 0;
}

.preview-content h1 {
  font-size: 2rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3em;
}

.preview-content h2 { font-size: 1.6rem; }
.preview-content h3 { font-size: 1.3rem; }

.preview-content p { margin-bottom: 1em; color: #333; }

.preview-content code {
  background: #f2f2f7;
  padding: 0.2em 0.45em;
  border-radius: 6px;
  font-family: 'SF Mono', monospace;
  font-size: 0.88em;
  color: #d63384;
}

.preview-content pre {
  background: #1e1e1e;
  padding: 18px 20px;
  border-radius: var(--radius-md);
  overflow-x: auto;
  margin: 1.2em 0;
  color: #e1e1e1;
}

.preview-content pre code {
  background: transparent;
  color: inherit;
  padding: 0;
  font-size: 0.9rem;
}

.preview-content blockquote {
  border-left: 3px solid #a1a1aa;
  padding: 12px 18px;
  color: #52525b;
  font-style: italic;
  margin: 1.2em 0;
  background: #fafafa;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.preview-content ul,
.preview-content ol {
  margin-left: 1.6em;
  margin-bottom: 1em;
}

.preview-content li {
  margin-bottom: 0.35em;
}

.preview-content a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid rgba(0,113,227,0.3);
}

.preview-content a:hover {
  border-bottom-color: currentColor;
}

.preview-content img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm);
  margin: 1em 0;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.preview-content video {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1em 0;
  border-radius: var(--radius-sm);
  background: #111;
}

.preview-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.2em 0;
  display: block;
  overflow-x: auto;
}

.preview-content th,
.preview-content td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
}

.preview-content th {
  background: #f5f5f7;
  font-weight: 600;
}

.parse-error {
  color: var(--danger);
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  padding: 16px;
}

/* ── Placeholder ── */
.placeholder-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 100%;
  min-height: 200px;
  color: #a1a1aa;
  font-size: 0.95rem;
  font-weight: 450;
  text-align: center;
  background: #fafafa;
  border-radius: var(--radius-md);
}

/* ── Drag over ── */
.app.drag-over { background: #f0f0f5; }

.app.drag-over .panel {
  background: #f8fafc;
}

/* ── Responsive ── */
@media (max-width: 700px) {
  .navbar {
    align-items: stretch;
    padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
  }

  .logo,
  .file-section,
  .view-control {
    width: 100%;
  }

  .file-section {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .file-btn,
  .view-btn {
    justify-content: center;
  }

  .file-badge,
  .md-selector {
    grid-column: 1 / -1;
    max-width: 100%;
  }

  .md-selector select {
    width: 100%;
    max-width: none;
  }

  .view-control {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  .view-btn { padding: 6px 8px; font-size: 0.8rem; }
  .panel { padding: 16px; }
  .split-view { flex-direction: column; }
  .split-view .panel {
    min-height: 0;
  }
  .split-view #rawPanel {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .raw-content {
    padding: 16px;
  }
  .preview-content h1 {
    font-size: 1.6rem;
  }
  .preview-content h2 {
    font-size: 1.35rem;
  }
}

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