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

:root {
  --bg: #f8f7f4;
  --card-bg: #ffffff;
  --accent: #2a7d5f;
  --accent-hover: #236b50;
  --text: #3a3a3a;
  --text-muted: #8a8a86;
  --border: #e0ddd8;
  --shadow: rgba(0, 0, 0, 0.06);
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --dot-color: var(--border);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1a1e;
    --card-bg: #242428;
    --accent: #3dab80;
    --accent-hover: #4fc492;
    --text: #e0e0e0;
    --text-muted: #7a7a80;
    --border: #3a3a40;
    --shadow: rgba(0, 0, 0, 0.25);
    --dot-color: #2e2e34;
  }
}

:root[data-theme="dark"] {
  --bg: #1a1a1e;
  --card-bg: #242428;
  --accent: #3dab80;
  --accent-hover: #4fc492;
  --text: #e0e0e0;
  --text-muted: #7a7a80;
  --border: #3a3a40;
  --shadow: rgba(0, 0, 0, 0.25);
  --dot-color: #2e2e34;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  height: 100vh;
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

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

.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

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

.btn-small {
  padding: 5px 12px;
  font-size: 13px;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  color: var(--text-muted);
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}

.btn-icon:hover {
  background: var(--bg);
  color: var(--text);
}

/* Home page */
.home {
  padding: 40px;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
  overflow-y: auto;
  flex: 1;
}

.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
}

.home-header h1 {
  font-size: 28px;
  font-weight: 600;
}

.subject-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}

.subject-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  cursor: pointer;
  transition: box-shadow 0.15s, border-color 0.15s;
  position: relative;
}

.subject-card:hover {
  box-shadow: 0 2px 12px var(--shadow);
  border-color: var(--accent);
}

.subject-card-name {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 8px;
}

.subject-card-meta {
  font-size: 13px;
  color: var(--text-muted);
}

.subject-card-actions {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.subject-card:hover .subject-card-actions {
  opacity: 1;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1.6;
}

/* Canvas toolbar */
.canvas-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 8px var(--shadow);
  z-index: 1000;
  flex-shrink: 0;
}

.toolbar-title {
  font-weight: 500;
  font-size: 15px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.zoom-display {
  font-size: 13px;
  color: var(--text-muted);
  min-width: 40px;
  text-align: right;
}

/* Canvas */
#canvas-viewport {
  flex: 1;
  overflow: hidden;
  position: relative;
  background:
    radial-gradient(circle, var(--dot-color) 1px, transparent 1px);
  background-size: 24px 24px;
}

#canvas-world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  width: 0;
  height: 0;
}

/* Notes */
.note {
  position: absolute;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 2px 8px var(--shadow);
  overflow: hidden;
  cursor: default;
  user-select: none;
  transition: box-shadow 0.15s;
}

.note:hover {
  box-shadow: 0 4px 16px var(--shadow);
}

.note.dragging {
  box-shadow: 0 8px 24px var(--shadow);
  opacity: 0.9;
}

.note-content {
  padding: 16px;
  overflow: auto;
  height: 100%;
  font-size: 14px;
  line-height: 1.6;
  user-select: text;
  cursor: text;
}

.note-content h1 { font-size: 20px; margin: 0 0 8px; }
.note-content h2 { font-size: 17px; margin: 0 0 6px; }
.note-content h3 { font-size: 15px; margin: 0 0 4px; }
.note-content p { margin: 0 0 8px; }
.note-content ul, .note-content ol { margin: 0 0 8px; padding-left: 20px; }
.note-content code {
  background: var(--bg);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 13px;
}
.note-content pre {
  background: var(--bg);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0 0 8px;
}
.note-content pre code {
  padding: 0;
  background: none;
}
.note-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 8px;
  font-size: 13px;
}
.note-content th,
.note-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.note-content th {
  background: var(--bg);
  font-weight: 600;
}
.note-content tr:hover td {
  background: var(--shadow);
}
.note-content a { color: var(--accent); }
.note-content mark {
  background: rgba(255, 224, 102, 0.4);
  padding: 1px 3px;
  border-radius: 3px;
}
.note-content .wikilink {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-style: dotted;
}
.note-content .katex-display {
  margin: 8px 0;
  overflow-x: auto;
}

.note-placeholder {
  color: var(--text-muted);
  font-style: italic;
}

.note-edit-container {
  height: 100%;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.edit-segment-text {
  flex-shrink: 0;
}

.note-editor {
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  padding: 12px 16px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.5;
  background: var(--card-bg);
  color: var(--text);
  min-height: 2em;
  overflow: hidden;
}

.note.editing {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(42, 125, 95, 0.2);
}

/* Table editor */
.edit-segment-table {
  flex-shrink: 0;
  padding: 4px 12px;
}

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

.table-editor th,
.table-editor td {
  border: 1px solid var(--border);
  padding: 18px 16px;
  outline: none;
  min-width: 160px;
  min-height: 60px;
  vertical-align: top;
}

.table-editor th {
  background: var(--bg);
  font-weight: 600;
}

.table-editor th:focus,
.table-editor td:focus {
  background: rgba(42, 125, 95, 0.06);
  box-shadow: inset 0 0 0 1.5px var(--accent);
}

.table-editor .table-add-col {
  width: 28px;
  text-align: center;
  cursor: pointer;
  color: var(--text-muted);
  font-weight: normal;
  user-select: none;
  border-style: dashed;
}

.table-editor .table-add-col:hover {
  background: rgba(42, 125, 95, 0.08);
  color: var(--accent);
}


.table-add-row {
  text-align: center;
  padding: 4px;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  border: 1px dashed var(--border);
  border-top: none;
  border-radius: 0 0 6px 6px;
  user-select: none;
}

.table-add-row:hover {
  background: rgba(42, 125, 95, 0.06);
  color: var(--accent);
}

/* Image notes */
.note-image {
  background: transparent;
  border: 1px solid transparent;
}

.note-image:hover {
  border-color: var(--accent);
}

.note-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: var(--radius);
  pointer-events: none;
}

/* Resize handle */
.note-resize-handle {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 16px;
  height: 16px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity 0.15s;
}

.note-resize-handle::after {
  content: '';
  position: absolute;
  bottom: 4px;
  right: 4px;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
}

.note:hover .note-resize-handle {
  opacity: 1;
}

/* Links SVG */
#link-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
  z-index: 0;
}

.link-line {
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linecap: round;
  pointer-events: none;
}

.link-hit-area {
  stroke: transparent;
  stroke-width: 12;
  stroke-linecap: round;
  pointer-events: stroke;
  cursor: pointer;
}

.link-line.link-hover {
  stroke: #e74c3c;
}

/* Link mode */
.link-mode {
  cursor: crosshair !important;
}

.link-mode .note {
  cursor: crosshair !important;
}

.note.link-source {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 3px rgba(42, 125, 95, 0.3) !important;
}

.btn-active {
  background: var(--accent) !important;
  color: white !important;
  border-color: var(--accent) !important;
}

/* Context menu */
.context-menu {
  position: fixed;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px var(--shadow);
  min-width: 140px;
  z-index: 10000;
  overflow: hidden;
}

.context-menu-item {
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.1s;
}

.context-menu-item:hover {
  background: var(--bg);
}

/* Responsive */
@media (max-width: 600px) {
  .home {
    padding: 20px;
  }

  .home-header h1 {
    font-size: 22px;
  }

  .subject-grid {
    grid-template-columns: 1fr;
  }
}
