:root {
  --bg-page: #f5f5f7;
  --bg-card: #ffffff;
  --border: #dcdce2;
  --border-subtle: #f0f0f4;
  --text-main: #19191c;
  --text-muted: #66666e;
  --primary: #2e73d9;
  --primary-hover: #235eb8;
  --btn-sec-bg: #e6e6eb;
  --btn-sec-hover: #dadade;
  --btn-sec-text: #2c2c32;
  --input-bg: #ffffff;
  --eq-bg: #fafafc;
  --eq-border: #e4e4ea;
  --coord-bg: #f0f0f4;
  --stroke-red: #d91919;
  --ideal-green: #26b340;
  --warn-orange: #bf5919;
  --save-green: #2c7a2c;
  --tooltip-bg: #222328;
  --tooltip-text: #ffffff;
}

[data-theme="dark"] {
  --bg-page: #111216;
  --bg-card: #1b1d24;
  --border: #2e313d;
  --border-subtle: #262934;
  --text-main: #ededf2;
  --text-muted: #989aa6;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --btn-sec-bg: #282b36;
  --btn-sec-hover: #333745;
  --btn-sec-text: #e4e5ec;
  --input-bg: #15171d;
  --eq-bg: #15171d;
  --eq-border: #2b2e3a;
  --coord-bg: #252832;
  --stroke-red: #ff4747;
  --ideal-green: #2ee653;
  --warn-orange: #f59e0b;
  --save-green: #4ade80;
  --tooltip-bg: #323644;
  --tooltip-text: #f3f4f8;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-page);
  color: var(--text-main);
  height: 100vh;
  overflow: hidden;
  transition: background 0.18s ease, color 0.18s ease;
}

.app-layout {
  display: grid;
  grid-template-columns: 1fr 410px;
  gap: 14px;
  padding: 12px;
  height: 100vh;
}

.canvas-panel {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px 14px;
  min-width: 0;
  min-height: 0;
  transition: background 0.18s ease, border-color 0.18s ease;
}

.canvas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.canvas-legend {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 12.5px;
  color: var(--text-muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.dot-user {
  background: var(--stroke-red);
}

.dot-ideal {
  background: var(--ideal-green);
}

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

.coord-readout {
  font-family: Consolas, monospace;
  font-size: 12px;
  background: var(--coord-bg);
  padding: 4px 8px;
  border-radius: 5px;
  min-width: 128px;
  text-align: right;
  color: var(--text-muted);
}

.btn-theme {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--btn-sec-bg);
  color: var(--btn-sec-text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease;
}

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

.canvas-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  position: relative;
}

#drawCanvas {
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: crosshair;
  touch-action: none;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.06);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.btn {
  border: none;
  border-radius: 7px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease, transform 0.05s ease;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  height: 48px;
  background: var(--primary);
  color: #ffffff;
  font-size: 16px;
  font-weight: 700;
}

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

.btn-primary:disabled {
  opacity: 0.65;
  cursor: wait;
}

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.btn-secondary {
  height: 36px;
  background: var(--btn-sec-bg);
  color: var(--btn-sec-text);
  font-size: 13.5px;
  font-weight: 500;
  padding: 0 12px;
}

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

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  transition: background 0.18s ease, border-color 0.18s ease;
}

.card-compact {
  padding: 6px 10px;
}

.card-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.ans-title {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.2;
  min-height: 27px;
  color: var(--text-main);
}

.ans-title.is-scribble {
  color: var(--warn-orange);
}

.ans-conf {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 3px;
  min-height: 18px;
}

.ans-status {
  font-size: 12px;
  color: var(--warn-orange);
  margin-top: 4px;
  min-height: 16px;
}

.top-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.top-table th {
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 12px;
  padding: 3px 4px 5px;
  border-bottom: 1px solid var(--border);
}

.top-table td {
  padding: 5px 4px;
  border-bottom: 1px solid var(--border-subtle);
}

.top-table tr:last-child td {
  border-bottom: none;
}

.top-table .col-pct {
  width: 68px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.empty-row td {
  color: var(--text-muted);
  text-align: center;
  padding: 12px 4px;
  font-size: 12.5px;
}

.preview-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: background 0.18s ease, border-color 0.18s ease;
}

.section-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

#previewCanvas {
  width: 136px;
  height: 136px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #000;
  align-self: center;
  image-rendering: pixelated;
}

.select-input {
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 8px;
  font-size: 13px;
  font-family: inherit;
  background: var(--input-bg);
  color: var(--text-main);
}

.save-row {
  display: grid;
  grid-template-columns: 1fr 105px;
  gap: 8px;
  margin-top: 4px;
}

.saved-status {
  font-size: 12px;
  color: var(--save-green);
  margin-top: 5px;
  min-height: 16px;
}

.eq-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100px;
}

.card-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.copy-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 12.5px;
  cursor: pointer;
  font-weight: 500;
  font-family: inherit;
}

.btn-link:hover {
  text-decoration: underline;
}

.info-badge {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--btn-sec-bg);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
  font-style: italic;
  font-family: Georgia, serif;
  cursor: help;
  user-select: none;
}

.info-tooltip {
  position: absolute;
  right: 0;
  bottom: calc(100% + 7px);
  background: var(--tooltip-bg);
  color: var(--tooltip-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-style: normal;
  font-weight: 400;
  font-size: 11.5px;
  line-height: 1.35;
  padding: 5px 9px;
  border-radius: 6px;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.22);
  opacity: 0;
  visibility: hidden;
  transform: translateY(3px);
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
  pointer-events: none;
  z-index: 20;
}

.info-badge:hover .info-tooltip,
.info-badge:focus .info-tooltip {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.eq-latex-box {
  flex: 1;
  width: 100%;
  border: 1px solid var(--eq-border);
  border-radius: 6px;
  padding: 10px 12px;
  background: var(--eq-bg);
  overflow-y: auto;
  overflow-x: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-main);
}

.eq-latex-box.is-empty {
  color: var(--text-muted);
  font-size: 12.5px;
  justify-content: flex-start;
  align-items: flex-start;
}

.eq-latex-box .katex-display {
  margin: 0;
  text-align: left;
  white-space: normal;
}

.eq-latex-box .katex {
  font-size: 1.04em;
  white-space: normal;
  color: var(--text-main);
}

@media (max-width: 960px) {
  body {
    height: auto;
    overflow: auto;
  }
  .app-layout {
    grid-template-columns: 1fr;
    height: auto;
  }
  .canvas-stage {
    min-height: 360px;
  }
}
