* { box-sizing: border-box; }

@font-face {
  font-family: "CMU Serif";
  src: url("https://cdn.jsdelivr.net/npm/computer-modern-web-font@1.0.0/fonts/serif/cmunrm.woff");
}

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --secondary: #64748b;
  --bg: #f8fafc;
  --panel-bg: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-light: #64748b;
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
  --radius: 8px;
  --danger: #ef4444;
  --success: #10b981;
  --warning: #f59e0b;
  
  /* Vở ô ly chuẩn */
  --cell-size: 15px;
  --grid-line: rgba(143, 161, 198, 0.6);
  --bold-line: rgba(123, 143, 189, 0.8);
  --margin-red: #d45b5b;
  --notebook-bg: white;
  --block-size: calc(var(--cell-size) * 4);
}

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.toolbar {
  background: white;
  padding: 12px 20px;
  display: flex;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  flex-wrap: wrap;
  align-items: center;
  z-index: 1000;
}

.toolbar-group {
  display: flex;
  gap: 8px;
  padding: 0 12px;
  border-right: 1px solid var(--border);
}

.toolbar-group:last-child {
  border-right: none;
}

.toolbar button {
  background: white;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}

.toolbar button:hover {
  background: #f1f5f9;
  border-color: var(--primary);
  transform: translateY(-1px);
}

.toolbar button.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2);
}

.toolbar button#exportBtn {
  background: var(--success);
  color: white;
  border: none;
  font-weight: 600;
  padding: 8px 20px;
}

.toolbar button#exportBtn:disabled {
  background: #cbd5e1;
  cursor: not-allowed;
  transform: none;
}

.toolbar button#exportBtn:not(:disabled):hover {
  background: #0da271;
}

.background-selector {
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 0 12px;
  border-right: 1px solid var(--border);
}

.background-option {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 2px solid transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.background-option:hover {
  transform: scale(1.1);
}

.background-option.active {
  border-color: var(--primary);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.background-option i {
  font-size: 16px;
  color: #64748b;
}

.workspace {
  display: grid;
  grid-template-columns: 1fr 320px;
  height: calc(100vh - 60px);
}

@media (max-width: 1024px) {
  .workspace {
    grid-template-columns: 1fr 300px;
  }
}

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

#canvas-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  background: white;
}

#canvas {
  width: 100%;
  height: 100%;
  position: relative;
  cursor: default;
  overflow: hidden;
}

#canvas.grid-bg {
  background: 
    linear-gradient(var(--border) 1px, transparent 1px),
    linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 15px 15px;
}

#canvas.notebook-bg {
  background-color: var(--notebook-bg);
  background-image:
    repeating-linear-gradient(
      to right,
      transparent 0,
      transparent calc(var(--cell-size) - 1px),
      var(--grid-line) calc(var(--cell-size) - 1px),
      var(--grid-line) var(--cell-size)
    ),
    repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent calc(var(--cell-size) - 1px),
      var(--grid-line) calc(var(--cell-size) - 1px),
      var(--grid-line) var(--cell-size)
    );
  position: relative;
}

#canvas.notebook-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent calc(var(--block-size) - 2px),
      var(--bold-line) calc(var(--block-size) - 2px),
      var(--bold-line) var(--block-size)
    );
  pointer-events: none;
  opacity: 0.5;
}

#canvas.notebook-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    repeating-linear-gradient(
      to right,
      transparent 0,
      transparent calc(var(--block-size) - 2px),
      var(--bold-line) calc(var(--block-size) - 2px),
      var(--bold-line) var(--block-size)
    );
  pointer-events: none;
  opacity: 0.5;
}

.notebook-margin {
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(var(--cell-size) * 3);
  width: calc(var(--cell-size) * 0.5);
  z-index: 0;
  pointer-events: none;
  opacity: 0.5;
}

.notebook-margin::before,
.notebook-margin::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--margin-red);
}

.notebook-margin::before { left: 0; }
.notebook-margin::after { right: 0; }

#canvas.plain-bg {
  background: white;
}

#canvas.text-mode {
  cursor: text;
}

.panel {
  background: var(--panel-bg);
  border-left: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#hint {
  font-size: 14px;
  color: var(--text-light);
  padding: 12px;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border-radius: var(--radius);
  border-left: 3px solid var(--primary);
  line-height: 1.5;
  margin: 0;
}

.panel-section {
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.panel-section h4 {
  margin: 0 0 16px 0;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.control {
  margin-bottom: 16px;
}

.control label {
  display: block;
  font-size: 14px;
  margin-bottom: 8px;
  color: var(--text);
  font-weight: 500;
}

.control input[type="range"] {
  width: 100%;
  height: 6px;
  -webkit-appearance: none;
  background: #e2e8f0;
  border-radius: 3px;
  outline: none;
  margin: 10px 0;
}

.control input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  background: var(--primary);
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.control input[type="range"]::-webkit-slider-thumb:hover {
  background: var(--primary-dark);
  transform: scale(1.1);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.control-row {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 8px;
  justify-content: space-between;
}

.control-row input[type="number"] {
  width: 80px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  text-align: center;
  transition: all 0.2s;
}

.control-row input[type="number"]:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.control input[type="color"] {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  padding: 2px;
  transition: all 0.2s;
}

.control input[type="color"]:hover {
  border-color: var(--primary);
}

.control input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.control input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 4px;
}

.control textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  resize: vertical;
  min-height: 60px;
  font-family: inherit;
  line-height: 1.4;
  transition: all 0.2s;
}

.control textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.control.checkbox {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
}

.control.checkbox input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin: 0;
  cursor: pointer;
  accent-color: var(--primary);
  border-radius: 4px;
}

.control.checkbox label {
  margin: 0;
  cursor: pointer;
  user-select: none;
  flex: 1;
}

.font-controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 8px;
}

.font-controls button {
  padding: 8px;
  border: 1px solid var(--border);
  background: white;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 40px;
}

.font-controls button:hover {
  border-color: var(--primary);
  background: #f8fafc;
  transform: translateY(-1px);
}

.font-controls button.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2);
}

.font-selector {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 8px;
}

.font-option {
  padding: 12px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  cursor: pointer;
  font-size: 13px;
  text-align: center;
  transition: all 0.2s;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
}

.font-option:hover {
  border-color: var(--primary);
  background: #f8fafc;
  transform: translateY(-1px);
}

.font-option.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2);
}

.font-option[data-font="arial"] {
  font-family: Arial, sans-serif;
}

.font-option[data-font="times"] {
  font-family: 'Times New Roman', serif;
}

.font-option[data-font="latex"] {
  font-family: 'CMU Serif', serif;
}

.text-element {
  position: absolute;
  cursor: move;
  user-select: none;
  z-index: 100;
  border-radius: 4px;
  min-width: 60px;
  min-height: 40px;
  background: transparent;
  border: 2px solid transparent;
  box-shadow: none;
  overflow: visible;
  touch-action: none;
}

.text-element:hover {
  border-color: rgba(37, 99, 235, 0.3);
}

.text-element.selected {
  border-color: var(--primary);
  background: rgba(37, 99, 235, 0.05);
}

.text-element.dragging {
  opacity: 0.5;
  border-color: var(--primary);
  z-index: 1000;
}

.text-resize-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: var(--primary);
  border: 2px solid white;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 101;
}

.text-element:hover .text-resize-handle,
.text-element.selected .text-resize-handle {
  opacity: 1;
}

.text-resize-handle:hover {
  background: var(--primary-dark);
}

.text-resize-handle.nw {
  top: -6px;
  left: -6px;
  cursor: nw-resize;
}

.text-resize-handle.ne {
  top: -6px;
  right: -6px;
  cursor: ne-resize;
}

.text-resize-handle.sw {
  bottom: -6px;
  left: -6px;
  cursor: sw-resize;
}

.text-resize-handle.se {
  bottom: -6px;
  right: -6px;
  cursor: se-resize;
}

.text-content {
  outline: none;
  resize: none;
  border: none;
  background: transparent;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 8px;
  font-family: inherit;
  line-height: 1.4;
  overflow: auto;
  cursor: text;
  color: inherit;
}

.text-content:focus {
  outline: none;
}

.text-delete-btn {
  position: absolute;
  top: -12px;
  right: -12px;
  background: var(--danger);
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 12px;
  opacity: 0;
  transition: all 0.2s;
  z-index: 102;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.text-element:hover .text-delete-btn,
.text-element.selected .text-delete-btn {
  opacity: 1;
}

.text-delete-btn:hover {
  background: #dc2626;
  transform: scale(1.1);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.segment-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 50;
}

.segment-svg * {
  pointer-events: auto;
}

.segment-hit-area {
  fill: transparent;
  cursor: move;
}

.segment-hit-area:hover {
  fill: rgba(37, 99, 235, 0.05);
}

.segment-line {
  pointer-events: none;
}

.curve-path {
  cursor: pointer;
  transition: all 0.2s;
  stroke-width: 2;
  stroke-linecap: round;
}

.curve-path:hover {
  stroke-width: 3px;
  filter: drop-shadow(0 0 2px rgba(37, 99, 235, 0.3));
}

.curve-path.selected {
  stroke-width: 3px;
  filter: drop-shadow(0 0 3px rgba(37, 99, 235, 0.5));
}

/* Enhanced curve hit area - QUAN TRỌNG: Fix bug chọn curve */
.curve-hit-area {
  fill: transparent;
  stroke: transparent;
  stroke-width: 20px; /* TĂNG LÊN rất lớn để dễ chọn */
  cursor: pointer;
}

.control-button {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.control-button.primary {
  background: var(--primary);
  color: white;
}

.control-button.primary:hover {
  background: var(--primary-dark);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(37, 99, 235, 0.2);
}

.control-button.danger {
  background: var(--danger);
  color: white;
}

.control-button.danger:hover {
  background: #dc2626;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(239, 68, 68, 0.2);
}

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: white;
  padding: 12px 24px;
  border-radius: var(--radius);
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 9999;
  text-align: center;
  max-width: 90vw;
  backdrop-filter: blur(10px);
  background: rgba(15, 23, 42, 0.9);
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 768px) {
  .toolbar {
    padding: 10px;
    gap: 6px;
    flex-wrap: nowrap;
    overflow-x: auto;
  }
  
  .toolbar-group {
    padding: 0 8px;
    flex-shrink: 0;
  }
  
  .toolbar button {
    padding: 7px 12px;
    font-size: 13px;
  }
  
  .background-selector {
    padding: 0 8px;
  }
  
  .panel {
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 14px;
    max-height: 40vh;
  }
  
  .workspace {
    grid-template-rows: 1fr auto;
  }
  
  .control-row input[type="number"] {
    width: 70px;
  }
  
  .font-controls,
  .font-selector {
    grid-template-columns: repeat(3, 1fr);
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-light);
  text-align: center;
  padding: 40px;
  background: transparent;
  pointer-events: none;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
  color: var(--primary);
}

.empty-state h3 {
  margin: 0 0 8px 0;
  font-weight: 600;
  color: var(--text);
}

.empty-state p {
  margin: 0;
  font-size: 14px;
  max-width: 300px;
}