:root {
  --bg: #f7f8fb;
  --surface: #ffffff;
  --surface-soft: #f2f4f8;
  --text: #111827;
  --muted: #6b7280;
  --subtle: #9ca3af;
  --border: #d9dee8;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #059669;
  --danger: #dc2626;
  --warning: #f97316;
  --radius: 8px;
  --shadow: 0 14px 40px rgba(17, 24, 39, 0.08);
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.5;
  letter-spacing: 0;
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: 56px;
  display: grid;
  grid-template-columns: minmax(150px, 1fr) auto minmax(150px, 1fr);
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  background: rgba(247, 248, 251, 0.96);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  justify-self: start;
}

.brand-mark {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  background: var(--text);
  color: #fff;
  font-weight: 800;
  line-height: 1;
}

.brand-name {
  font-size: 15px;
  font-weight: 750;
}

.topbar-links {
  justify-self: center;
  display: inline-flex;
  align-items: center;
  gap: 14px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.topbar-links a:hover {
  color: var(--text);
}

.session-info {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.session-user {
  max-width: 180px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-layout {
  min-height: calc(100vh - 56px);
  padding: 18px;
}

.auth-panel {
  width: min(420px, calc(100vw - 36px));
  margin: 9vh auto 0;
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.auth-copy h1,
.pane-head h1 {
  margin: 0;
  font-size: 17px;
  line-height: 1.25;
  letter-spacing: 0;
}

.auth-copy p,
.pane-head p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin-top: 18px;
  padding: 4px;
  background: var(--surface-soft);
  border-radius: var(--radius);
}

.auth-tab,
.segment {
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.auth-tab.active,
.segment.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(17, 24, 39, 0.08);
}

.auth-form,
.field {
  display: grid;
  gap: 8px;
}

.auth-form {
  margin-top: 16px;
}

.field {
  min-width: 0;
}

.field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.field-fill {
  min-height: 0;
}

.control,
.source-input {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.control {
  height: 38px;
  padding: 0 11px;
  font-size: 13px;
}

.source-input {
  flex: 1;
  min-height: 280px;
  height: 100%;
  resize: none;
  padding: 12px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
}

.control:focus,
.source-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.workspace {
  height: calc(100vh - 92px);
  min-height: 620px;
  display: grid;
  grid-template-columns: minmax(340px, 0.95fr) minmax(360px, 1.1fr) minmax(260px, 320px);
  gap: 14px;
}

.editor-pane,
.preview-pane,
.history-pane {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.preview-pane,
.history-pane {
  grid-template-rows: auto minmax(0, 1fr);
}

.pane-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.pane-head.compact {
  align-items: center;
}

.segmented {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 96px 72px;
  gap: 4px;
  padding: 4px;
  background: var(--surface-soft);
  border-radius: var(--radius);
}

.primary-button,
.secondary-button,
.icon-button {
  border: 0;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 750;
}

.primary-button {
  min-height: 38px;
  padding: 0 16px;
  background: var(--primary);
  color: #fff;
}

.primary-button:hover {
  background: var(--primary-dark);
}

.secondary-button {
  min-height: 38px;
  padding: 0 14px;
  background: var(--surface-soft);
  color: var(--text);
}

.secondary-button:hover {
  background: #e8edf6;
}

.icon-button {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  display: inline-grid;
  place-items: center;
  background: var(--surface-soft);
  color: var(--text);
}

.icon-button:hover {
  background: #e8edf6;
}

.icon-button.danger {
  color: var(--danger);
}

.icon-button svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.preview-frame {
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
}

.markdown-preview,
.html-preview {
  width: 100%;
  height: 100%;
  min-height: 0;
}

.markdown-preview {
  overflow: auto;
  padding: 18px;
  color: #1f2937;
  font-size: 14px;
  line-height: 1.65;
}

.html-preview {
  display: block;
  border: 0;
  background: #fff;
}

.markdown-preview h1,
.markdown-preview h2,
.markdown-preview h3 {
  margin: 1em 0 0.45em;
  line-height: 1.25;
}

.markdown-preview h1:first-child,
.markdown-preview h2:first-child,
.markdown-preview h3:first-child,
.markdown-preview p:first-child,
.markdown-preview ul:first-child,
.markdown-preview ol:first-child,
.markdown-preview pre:first-child,
.markdown-preview blockquote:first-child {
  margin-top: 0;
}

.markdown-preview h1 {
  font-size: 26px;
}

.markdown-preview h2 {
  font-size: 21px;
}

.markdown-preview h3 {
  font-size: 17px;
}

.markdown-preview p,
.markdown-preview ul,
.markdown-preview ol,
.markdown-preview pre,
.markdown-preview blockquote {
  margin: 0.8em 0;
}

.markdown-preview ul,
.markdown-preview ol {
  padding-left: 1.4em;
}

.markdown-preview code {
  padding: 2px 5px;
  border-radius: 5px;
  background: #eef2f7;
  font-family: var(--mono);
  font-size: 0.92em;
}

.markdown-preview pre {
  overflow: auto;
  padding: 12px;
  border-radius: var(--radius);
  background: #0f172a;
  color: #e5e7eb;
}

.markdown-preview pre code {
  padding: 0;
  background: transparent;
  color: inherit;
}

.markdown-preview blockquote {
  padding: 8px 12px;
  border-left: 3px solid var(--primary);
  background: #f3f7ff;
  color: #374151;
}

.markdown-preview a {
  color: var(--primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.history-list {
  min-height: 0;
  display: grid;
  align-content: start;
  gap: 8px;
  overflow: auto;
}

.history-empty {
  padding: 18px 12px;
  color: var(--muted);
  background: var(--surface-soft);
  border-radius: var(--radius);
  font-size: 13px;
  text-align: center;
}

.history-item {
  width: 100%;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  text-align: left;
}

.history-item.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.history-main {
  min-width: 0;
  border: 0;
  background: transparent;
  color: inherit;
  padding: 0;
  text-align: left;
}

.history-title {
  display: block;
  overflow: hidden;
  color: var(--text);
  font-size: 13px;
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-meta,
.history-snippet {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-snippet {
  margin-top: 4px;
}

.history-delete {
  align-self: start;
  width: 28px;
  height: 28px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 50;
  width: min(420px, calc(100vw - 28px));
  transform: translateX(-50%);
  padding: 11px 14px;
  border-radius: var(--radius);
  background: #111827;
  color: #fff;
  box-shadow: var(--shadow);
  font-size: 13px;
  font-weight: 650;
  text-align: center;
}

@media (max-width: 1180px) {
  .workspace {
    height: auto;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr) minmax(280px, 340px);
  }

  .preview-pane {
    min-height: 520px;
  }

  .history-pane {
    grid-column: 2;
    grid-row: 1 / span 2;
  }
}

@media (max-width: 820px) {
  .topbar {
    grid-template-columns: minmax(110px, 1fr) auto;
    padding: 0 14px;
  }

  .topbar-links {
    display: none;
  }

  .app-layout {
    padding: 12px;
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .history-pane {
    grid-column: auto;
    grid-row: auto;
    min-height: 320px;
  }

  .editor-pane,
  .preview-pane,
  .history-pane {
    padding: 13px;
  }

  .pane-head {
    display: grid;
  }

  .segmented {
    width: 100%;
    grid-template-columns: 1fr 1fr;
  }

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

  .source-input {
    min-height: 300px;
  }
}
