/* ===== CSS Custom Properties ===== */
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-tertiary: #e9ecef;
  --text-primary: #212529;
  --text-secondary: #495057;
  --text-muted: #868e96;
  --border-color: #dee2e6;
  --accent: #4263eb;
  --accent-hover: #3b5bdb;
  --accent-light: #dbe4ff;
  --success: #2b8a3e;
  --error: #e03131;
  --header-bg: #1e293b;
  --header-text: #f1f3f5;
  --editor-bg: #ffffff;
  --editor-border: #dee2e6;
  --btn-ghost-hover: rgba(0, 0, 0, 0.06);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07);
  --radius: 6px;
  --radius-lg: 14px;
  --transition: 150ms ease;
  /* macOS 26+ glass and capsule tokens */
  --header-glass: rgba(6, 18, 38, 0.9);   /* reads as #1e293b over the light page */
  --blur: blur(24px) saturate(170%);
  --hairline: rgba(0, 0, 0, 0.1);
  --fill: rgba(0, 0, 0, 0.05);
  --fill-hover: rgba(0, 0, 0, 0.08);
  --seg-track: rgba(0, 0, 0, 0.06);
  --seg-thumb: #ffffff;
  --btn-secondary-bg: #ffffff;
  --btn-secondary-rim: inset 0 0 0 0.5px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.06);
  --accent-ink: #ffffff;
  --accent-glow: rgba(66, 99, 235, 0.28);
  --ring: rgba(66, 99, 235, 0.22);
  --toast-ink: #ffffff;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg-primary: #1a1b26;
  --bg-secondary: #24283b;
  --bg-tertiary: #343b58;
  --text-primary: #c0caf5;
  --text-secondary: #a9b1d6;
  --text-muted: #565f89;
  --border-color: #343b58;
  --accent: #7aa2f7;
  --accent-hover: #89b4fa;
  --accent-light: #292e42;
  --success: #9ece6a;
  --error: #f7768e;
  --header-bg: #16161e;
  --header-text: #c0caf5;
  --editor-bg: #1a1b26;
  --editor-border: #343b58;
  --btn-ghost-hover: rgba(255, 255, 255, 0.08);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
  --header-glass: rgba(17, 17, 24, 0.82); /* reads as #16161e over the dark page */
  --hairline: rgba(255, 255, 255, 0.09);
  --fill: rgba(255, 255, 255, 0.07);
  --fill-hover: rgba(255, 255, 255, 0.12);
  --seg-track: rgba(0, 0, 0, 0.25);
  --seg-thumb: rgba(255, 255, 255, 0.12);
  --btn-secondary-bg: rgba(255, 255, 255, 0.07);
  --btn-secondary-rim: inset 0 0.5px 0 rgba(255, 255, 255, 0.14);
  --accent-ink: #16161e;
  --accent-glow: rgba(122, 162, 247, 0.26);
  --ring: rgba(122, 162, 247, 0.25);
  --toast-ink: #16161e;
  color-scheme: dark;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  background: var(--bg-secondary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background var(--transition), color var(--transition);
}

/* ===== Header ===== */
.header {
  position: sticky;
  top: 0;
  background: var(--header-glass);
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
  color: var(--header-text);
  padding: 0 14px 0 20px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: inset 0 -0.5px 0 rgba(255, 255, 255, 0.08), var(--shadow-md);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  color: var(--accent);
}

.header-title {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-right {
  display: flex;
  align-items: center;
}

/* ===== Icon Button ===== */
.icon-btn {
  background: none;
  border: none;
  color: var(--header-text);
  cursor: pointer;
  padding: 8px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), transform var(--transition);
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

.icon-btn:active {
  transform: scale(0.94);
}

/* Theme toggle icon visibility */
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

/* ===== Tab Navigation ===== */
/* Format | Diff: a segmented control, recessed track with a raised thumb */
.tab-nav {
  align-self: flex-start;
  margin: 16px 20px 0;
  padding: 3px;
  border-radius: 999px;
  background: var(--seg-track);
  box-shadow: inset 0 0 0 0.5px var(--hairline);
  display: flex;
  position: relative;
  gap: 2px;
}

.tab-btn {
  background: none;
  border: none;
  border-radius: 999px;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  padding: 6px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: color var(--transition), background-color var(--transition);
  position: relative;
}

.tab-btn:hover {
  color: var(--text-primary);
}

.tab-btn.active {
  color: var(--accent);
  background: var(--seg-thumb);
  box-shadow: inset 0 0.5px 0 rgba(255, 255, 255, 0.3), 0 1px 3px rgba(0, 0, 0, 0.14);
}

.tab-btn svg {
  opacity: 0.7;
}

.tab-btn.active svg {
  opacity: 1;
}

/* ===== Panels ===== */
.panel {
  display: none;
  flex-direction: column;
  flex: 1;
  padding: 16px 20px;
  gap: 16px;
  min-height: 0;
}

.panel.active {
  display: flex;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.toolbar-select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--btn-secondary-bg);
  border: 0;
  box-shadow: var(--btn-secondary-rim);
  color: var(--text-primary);
  padding: 7px 30px 7px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23868e96' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 11px center;
  transition: box-shadow var(--transition);
}

.toolbar-select:focus {
  outline: none;
  box-shadow: inset 0 0 0 1px var(--accent), 0 0 0 4px var(--ring);
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 999px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  border: 0;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn:focus-visible,
.icon-btn:focus-visible,
.tab-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  box-shadow: inset 0 0.5px 0 rgba(255, 255, 255, 0.45), 0 4px 14px var(--accent-glow);
}

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

.btn-secondary {
  background: var(--btn-secondary-bg);
  color: var(--text-primary);
  box-shadow: var(--btn-secondary-rim);
}

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

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--btn-ghost-hover);
  color: var(--text-primary);
}

/* ===== Editors Grid ===== */
.editors-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  flex: 1;
  min-height: 0;
}

.editors-grid--triple {
  grid-template-columns: 1fr 1fr 1fr;
}

.editor-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.editor-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
}

/* ===== Editor Label Bar (3-editor mode) ===== */
.editor-label-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  height: 28px;
}

.primary-toggle {
  display: flex;
  align-items: center;
  cursor: pointer;
  flex-shrink: 0;
}

.primary-radio {
  display: none;
}

.primary-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--border-color);
  transition: all var(--transition);
}

.primary-radio:checked + .primary-dot {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: inset 0 0 0 2px var(--bg-primary);
}

.editor-title-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 3px 8px;
  outline: none;
  transition: box-shadow var(--transition), background var(--transition);
}

.editor-title-input:hover {
  background: var(--fill);
}

.editor-title-input:focus {
  background: var(--bg-primary);
  color: var(--text-primary);
  box-shadow: inset 0 0 0 1px var(--accent), 0 0 0 3px var(--ring);
}

.primary-badge {
  display: none;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-light);
  padding: 1px 7px;
  border-radius: 9999px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  flex-shrink: 0;
}

.editor-pane.is-primary .primary-badge {
  display: inline-block;
}

.editor-pane.is-primary .editor-mount {
  border-left: 3px solid var(--accent);
}

.editor-mount {
  flex: 1;
  border: 0.5px solid var(--hairline);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--editor-bg);
  box-shadow: var(--shadow-sm);
  min-height: 300px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.editors-grid--triple .editor-mount {
  min-height: 250px;
}

.editor-mount:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}

/* ===== Diff Output Container ===== */
.diff-output-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.diff-output-container:empty {
  display: none;
}

.diff-result {
  border: 0.5px solid var(--hairline);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--bg-primary);
  box-shadow: var(--shadow-sm);
}

.diff-result-header {
  padding: 9px 16px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-secondary);
  border-bottom: 0.5px solid var(--hairline);
}

.diff-result-header .base-name {
  color: var(--accent);
}

.diff-result-body {
  min-height: 60px;
}

/* the card already draws the edge: no second border from diff2html inside it */
.diff-result-body .d2h-file-wrapper {
  border: 0;
  border-radius: 0;
  margin-bottom: 0;
}

/* CodeMirror overrides */
.editor-mount .cm-editor {
  height: 100%;
}

.editor-mount .cm-scroller {
  overflow: auto;
}

.editor-mount .cm-focused {
  outline: none !important;
}

.diff-no-changes {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* diff2html dark mode overrides */
[data-theme="dark"] .d2h-wrapper {
  background: var(--bg-primary);
  color: var(--text-primary);
}

[data-theme="dark"] .d2h-file-header {
  background: var(--bg-secondary);
  border-color: var(--border-color);
}

[data-theme="dark"] .d2h-file-wrapper {
  border-color: var(--border-color);
}

[data-theme="dark"] .d2h-code-linenumber,
[data-theme="dark"] .d2h-code-side-linenumber {
  background: var(--bg-secondary);
  color: var(--text-muted);
  border-color: var(--border-color);
}

[data-theme="dark"] .d2h-code-line,
[data-theme="dark"] .d2h-code-side-line {
  color: var(--text-primary);
}

[data-theme="dark"] .d2h-code-line-ctn {
  color: var(--text-primary);
}

[data-theme="dark"] .d2h-del {
  background: rgba(247, 118, 142, 0.15);
}

[data-theme="dark"] .d2h-ins {
  background: rgba(158, 206, 106, 0.15);
}

[data-theme="dark"] .d2h-del .d2h-code-line-ctn {
  background: rgba(247, 118, 142, 0.08);
}

[data-theme="dark"] .d2h-ins .d2h-code-line-ctn {
  background: rgba(158, 206, 106, 0.08);
}

[data-theme="dark"] .d2h-info {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border-color: var(--border-color);
}

[data-theme="dark"] .d2h-emptyplaceholder,
[data-theme="dark"] .d2h-code-side-emptyplaceholder {
  background: var(--bg-tertiary);
  border-color: var(--border-color);
}

/* ===== Status Bar ===== */
/* status: a floating capsule toast; the colour stays put until it has faded out */
.status-bar {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 20;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--toast-ink);
  display: flex;
  align-items: center;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 16px);
  box-shadow: inset 0 0.5px 0 rgba(255, 255, 255, 0.35), 0 10px 30px rgba(0, 0, 0, 0.18), 0 1px 3px rgba(0, 0, 0, 0.12);
  transition: opacity 200ms ease-out, transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1), background-color 0s linear 300ms;
}

.status-bar.visible {
  opacity: 1;
  transform: translate(-50%, 0);
  transition: opacity 200ms ease-out, transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.status-bar.success {
  background: var(--success);
}

.status-bar.error {
  background: var(--error);
}

/* ===== Responsive ===== */
@media (max-width: 1100px) and (min-width: 769px) {
  .editors-grid--triple {
    grid-template-columns: 1fr 1fr;
  }

  .editors-grid--triple > .editor-pane:nth-child(3) {
    grid-column: 1 / -1;
  }
}

@media (max-width: 768px) {
  .editors-grid {
    grid-template-columns: 1fr;
  }

  .editors-grid--triple {
    grid-template-columns: 1fr;
  }

  .toolbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .toolbar-actions {
    margin-left: 0;
    width: 100%;
    flex-wrap: wrap;
  }

  .panel {
    padding: 12px;
  }

  .header {
    padding: 0 6px 0 12px;
  }

  .tab-nav {
    margin: 12px 12px 0;
  }

  /* 16px stops iOS zooming in on focus */
  .toolbar-select,
  .editor-title-input {
    font-size: 16px;
  }

  .btn {
    padding: 7px 10px;
    font-size: 0.8rem;
  }
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel.active {
  animation: fadeIn 200ms ease;
}

/* ===== Scrollbar (dark mode) ===== */
[data-theme="dark"] ::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

[data-theme="dark"] ::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 4px;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ===== Reduce Transparency / Reduce Motion ===== */
@media (prefers-reduced-transparency: reduce) {
  .header {
    background: var(--header-bg);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .status-bar {
    transform: translate(-50%, 0);
    transition: opacity 200ms ease-out, background-color 0s linear 300ms;
  }

  .status-bar.visible {
    transition: opacity 200ms ease-out;
  }
}
