:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f8faff;
  --ink: #152033;
  --muted: #647087;
  --line: #d9e0ec;
  --accent: #2463eb;
  --accent-strong: #1749b3;
  --accent-soft: #edf3ff;
  --success: #16805e;
  --success-soft: #eaf8f2;
  --warning: #a65a00;
  --warning-soft: #fff6e6;
  --danger: #b4233f;
  --shadow: 0 12px 34px rgba(28, 43, 74, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="dark"],
html[data-theme="dark"] body,
html[data-site-theme="dark"],
html[data-site-theme="dark"] body,
html[data-theme="dark"] .app,
html[data-site-theme="dark"] .app,
html[data-theme="dark"] .panel,
html[data-site-theme="dark"] .panel,
html[data-theme="dark"] .workspace,
html[data-site-theme="dark"] .workspace {
  background-color: #0c111b !important;
  color: #e8edf6 !important;
}

html[data-site-theme="dark"], html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0c111b;
  --surface: #151d2a;
  --surface-2: #101722;
  --ink: #edf1f8;
  --muted: #a5afc0;
  --line: #303b4f;
  --accent: #7fa2f4;
  --accent-strong: #a9c1ff;
  --accent-soft: #1d2d50;
  --success: #72dbb4;
  --success-soft: #17372d;
  --warning: #f2c16f;
  --warning-soft: #3a2e19;
  --danger: #ff8d9a;
  --shadow: 0 14px 38px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--bg); font-size: 14px; }
button, textarea, select, input { font: inherit; }
button, select, input { color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.5; }
button:focus-visible, textarea:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 36%, transparent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app-header { display: flex; min-height: 74px; padding: 15px clamp(16px, 3vw, 36px); align-items: center; justify-content: space-between; gap: 18px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--surface) 94%, transparent); }
.app-brand, .header-actions, .card-heading, .card-heading > div, .action-row, .button-group, .alignment-group, .utility-group { display: flex; align-items: center; }
.app-brand { gap: 12px; }
.app-brand div { display: grid; gap: 2px; }
.app-brand strong { font-size: 16px; }
.app-brand small { color: var(--muted); }
.brand-mark { display: block; width: 40px; height: 40px; flex: 0 0 40px; border-radius: 11px; overflow: hidden; box-shadow: 0 7px 20px color-mix(in srgb, var(--accent) 28%, transparent); }
.brand-mark svg { display: block; width: 100%; height: 100%; }
.header-actions { gap: 12px; }
.local-pill { display: inline-flex; align-items: center; gap: 7px; color: var(--success); font-weight: 700; font-size: 12px; }
.local-pill span { width: 8px; height: 8px; border-radius: 50%; background: currentColor; box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 14%, transparent); }
.icon-button { display: grid; width: 38px; height: 38px; padding: 0; place-items: center; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: var(--surface); }
#theme-button::before { width: 16px; height: 16px; border: 2px solid currentColor; border-radius: 50%; content: ""; box-shadow: inset -5px -3px 0 -2px var(--surface), 5px -3px 0 -4px currentColor; }

.workspace { width: min(1400px, 100%); margin: 0 auto; padding: clamp(18px, 3vw, 34px); display: flex; flex-direction: column; gap: 20px; }

/* Toolbar */
.toolbar { display: flex; padding: 12px 16px; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 14px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); }
.mode-switch { display: inline-flex; padding: 4px; gap: 3px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); }
.mode-switch button { min-width: 95px; padding: 8px 15px; border: 0; border-radius: 7px; color: var(--muted); background: transparent; font-weight: 700; font-size: 13px; }
.mode-switch button.active { color: white; background: var(--accent); }

.action-row { flex-wrap: wrap; gap: 12px; }
.button-group { gap: 4px; }
.alignment-group { gap: 4px; padding: 0 8px; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
.alignment-label { font-weight: 700; color: var(--muted); font-size: 12px; margin-right: 4px; }
.align-btn.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); font-weight: 800; }
.utility-group { gap: 8px; }

.button { min-height: 38px; padding: 8px 14px; border: 1px solid var(--line); border-radius: 9px; color: var(--ink); background: var(--surface); font-weight: 700; font-size: 13px; display: inline-flex; align-items: center; justify-content: center; transition: all 0.15s ease; }
.button:hover:not(:disabled), .icon-button:hover { border-color: var(--accent); }
.button.primary { color: white; border-color: var(--accent); background: var(--accent); }
.button.primary:hover:not(:disabled) { background: var(--accent-strong); }
.button.quiet { color: var(--muted); background: transparent; }
.button.quiet:hover:not(:disabled) { color: var(--ink); background: var(--surface-2); }
.button.small { min-height: 32px; padding: 4px 10px; font-size: 12px; }

.compact-check { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; user-select: none; margin-left: 6px; }
.compact-check input { accent-color: var(--accent); cursor: pointer; }

/* Editor Grid Workspace */
.editor-workspace { border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; }
.table-container { width: 100%; overflow-x: auto; max-height: 480px; overflow-y: auto; background: var(--surface-2); }
.editor-table { width: 100%; border-collapse: collapse; table-layout: auto; font-size: 13px; }
.editor-table th, .editor-table td { border: 1px solid var(--line); padding: 0; margin: 0; min-width: 140px; }
.editor-table th { background: var(--surface); position: sticky; top: 0; z-index: 2; box-shadow: 0 1px 0 var(--line); }

.cell-header-wrap { display: flex; flex-direction: column; padding: 6px 8px; gap: 4px; }
.cell-header-top { display: flex; align-items: center; justify-content: space-between; font-size: 11px; color: var(--muted); font-weight: 700; }
.col-align-badge { font-family: ui-monospace, monospace; font-size: 10px; color: var(--accent); background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; text-transform: uppercase; }
.cell-input { display: block; width: 100%; padding: 8px 10px; border: 0; outline: 0; background: transparent; color: var(--ink); font-family: inherit; font-size: 13px; }
.cell-input:focus { background: var(--surface); box-shadow: inset 0 0 0 2px var(--accent); }
.cell-input.header-input { font-weight: 700; color: var(--accent-strong); }

.editor-table th.active-col, .editor-table td.active-col { background: color-mix(in srgb, var(--accent-soft) 40%, transparent); }

.table-hints { padding: 10px 16px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); background: var(--surface); }

/* Import Workspace */
.import-workspace { border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.import-card { display: flex; flex-direction: column; }
.card-heading { min-height: 61px; padding: 13px 18px; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
.step { display: grid; width: 27px; height: 27px; place-items: center; border-radius: 7px; color: var(--accent); background: var(--accent-soft); font: 700 11px/1 ui-monospace, monospace; }
.card-heading label, .card-heading h2 { margin: 0; color: var(--ink); font-weight: 750; font-size: 15px; }
.import-format-select { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); font-weight: 600; }
.import-format-select select { padding: 5px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); }

.import-card textarea { display: block; width: 100%; min-height: 240px; padding: 16px; border: 0; outline: 0; color: var(--ink); background: var(--surface-2); font: 13px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; resize: vertical; }
.import-card textarea:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.import-actions { display: flex; align-items: center; justify-content: space-between; padding: 12px 18px; border-top: 1px solid var(--line); background: var(--surface); }
.import-preview-msg { font-size: 12px; font-weight: 600; color: var(--muted); }

/* Output Section */
.output-section { border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.output-card { display: flex; flex-direction: column; }
.output-title { display: flex; align-items: center; gap: 10px; }
.output-view-toggle { display: inline-flex; padding: 3px; gap: 2px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--line); }
.view-tab { padding: 5px 12px; border: 0; border-radius: 6px; color: var(--muted); background: transparent; font-weight: 700; font-size: 12px; }
.view-tab.active { color: var(--ink); background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.export-actions { display: flex; gap: 8px; }

.output-content { position: relative; }
.output-content textarea { display: block; width: 100%; min-height: 220px; padding: 16px; border: 0; outline: 0; color: var(--ink); background: var(--surface-2); font: 13px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; resize: vertical; }

.rendered-table-view { padding: 20px; min-height: 220px; background: var(--surface-2); overflow-x: auto; }
.rendered-table-view table { width: 100%; border-collapse: collapse; margin: 0; font-size: 13px; }
.rendered-table-view th, .rendered-table-view td { border: 1px solid var(--line); padding: 10px 14px; text-align: left; }
.rendered-table-view th { background: var(--surface); font-weight: 700; color: var(--ink); }
.rendered-table-view td { background: var(--surface); }

/* Status / Toast Message */
.status-message { padding: 12px 16px; border-radius: 10px; font-weight: 600; font-size: 13px; border: 1px solid var(--line); }
.status-message.error { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, var(--surface)); border-color: var(--danger); }
.status-message.success { color: var(--success); background: var(--success-soft); border-color: var(--success); }

/* Metric Strip */
.metric-strip { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.metric-strip article { padding: 14px 16px; border-right: 1px solid var(--line); }
.metric-strip article:last-child { border-right: 0; }
.metric-strip span { display: block; margin-bottom: 6px; color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; }
.metric-strip strong { font-size: 18px; color: var(--ink); }

@media (max-width: 900px) {
  .metric-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .metric-strip article:nth-child(3) { border-right: 0; }
  .metric-strip article { border-bottom: 1px solid var(--line); }
  .alignment-group { border-left: 0; border-right: 0; padding: 0; }
}

@media (max-width: 640px) {
  .app-header { flex-direction: column; align-items: flex-start; gap: 12px; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .action-row { flex-direction: column; align-items: stretch; }
  .card-heading { flex-direction: column; align-items: flex-start; gap: 10px; }
  .metric-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .export-actions { width: 100%; }
  .export-actions button { flex: 1; }
}
