/* ============================================================
   Vẽ đồ thị phương trình — bảng màu & biến hệ thống
   Chủ đề: sổ tay kỹ thuật / giấy kẻ ô, mực xanh navy
   ============================================================ */
:root {
  --paper:      #f7f6f2;   /* nền ngà */
  --surface:    #ffffff;   /* panel, canvas */
  --line:       #e6e3db;   /* viền nhạt */
  --line-soft:  #efece5;
  --ink:        #1b2430;   /* chữ chính, trục */
  --ink-2:      #5b6673;   /* chữ phụ */
  --ink-3:      #8a94a2;   /* chữ mờ */
  --accent:     #1f4fd8;   /* mực xanh — nút, focus */
  --accent-ink: #16357f;
  --grid:       #eceef1;   /* lưới nhỏ */
  --grid-bold:  #dcdfe4;   /* lưới lớn */

  --c-linear:   #e5484d;   /* đường bậc nhất */
  --c-parab:    #8e4ec6;   /* parabola */
  --c-conic:    #1a9e8f;   /* bậc hai hai ẩn */
  --c-point:    #f5a300;   /* điểm đặc biệt */

  --danger-bg:  #fdecec;
  --danger-ink: #b42318;
  --danger-line:#f3b4b0;

  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", "JetBrains Mono", "Cascadia Code", "Roboto Mono", ui-monospace, "Consolas", monospace;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --shadow: 0 1px 2px rgba(20,30,45,.05), 0 8px 24px rgba(20,30,45,.06);
  --topbar-h: 56px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--paper);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Thanh trên ---------- */
.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}

.back {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px 8px 11px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s, transform .05s;
}
.back:hover { color: var(--ink); border-color: var(--grid-bold); background: var(--paper); }
.back:active { transform: scale(.97); }
.back:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(31, 79, 216, .28); }
.back svg { flex-shrink: 0; }

.readout {
  margin-left: auto;
  display: flex;
  gap: 8px;
  font-family: var(--font-mono);
  font-size: 12.5px;
}
.readout__pair {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 5px 10px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.readout__k { color: var(--accent); font-weight: 700; }
.readout__v { color: var(--ink-2); min-width: 44px; text-align: right; font-variant-numeric: tabular-nums; }

.topbar__toggle { margin-left: auto; }
.icon-btn.topbar__toggle { display: none; }

/* ---------- Nút icon dùng chung ---------- */
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px; height: 38px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  border-radius: 10px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s, transform .05s;
}
.icon-btn:hover { color: var(--ink); border-color: var(--grid-bold); background: var(--paper); }
.icon-btn:active { transform: scale(.94); }

/* ---------- Bố cục ---------- */
.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ---------- Bảng điều khiển ---------- */
.panel {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel__handle { display: none; }
.panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 8px;
}
.panel__head h2 { font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-3); }
.panel__close { display: none; width: 34px; height: 34px; }

.panel__body {
  padding: 8px 20px 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.field { display: flex; flex-direction: column; gap: 8px; }
.field label {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
}
.field__hint { font-size: 12px; color: var(--ink-3); line-height: 1.5; }
.field__hint code {
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0 5px;
  color: var(--ink-2);
}

/* Select tùy biến */
.select { position: relative; }
.select select {
  width: 100%;
  appearance: none;
  font-family: var(--font-ui);
  font-size: 13.5px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 11px 38px 11px 12px;
  cursor: pointer;
}
.select__caret {
  position: absolute; right: 11px; top: 50%;
  transform: translateY(-50%);
  color: var(--ink-3);
  pointer-events: none;
}

/* Ô nhập + nút vẽ */
.input-row { display: flex; gap: 8px; }
.input-row input {
  flex: 1;
  min-width: 0;
  font-family: var(--font-mono);
  font-size: 15px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 11px 12px;
}
.input-row input::placeholder { color: var(--ink-3); }

input:focus-visible,
select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 79, 216, .16);
}

.btn {
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background .15s, transform .05s, box-shadow .15s;
}
.btn--primary {
  background: var(--accent);
  color: #fff;
  padding: 11px 20px;
  box-shadow: 0 1px 2px rgba(31,79,216,.25);
}
.btn--primary:hover { background: var(--accent-ink); }
.btn--primary:active { transform: translateY(1px); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(31,79,216,.28); }

/* Cảnh báo lỗi */
.alert {
  font-size: 13px;
  line-height: 1.45;
  color: var(--danger-ink);
  background: var(--danger-bg);
  border: 1px solid var(--danger-line);
  border-radius: var(--r-sm);
  padding: 10px 12px;
}

/* Chip ví dụ */
.chips { display: flex; flex-direction: column; gap: 10px; }
.chips__label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.chips__list { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--ink-2);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
}
.chip:hover { color: var(--accent); border-color: var(--accent); background: #fff; }

/* Khối thông tin phân tích */
.info {
  border-top: 1px solid var(--line);
  padding-top: 18px;
}
.info__title {
  font-size: 12px; font-weight: 650; text-transform: uppercase;
  letter-spacing: .08em; color: var(--ink-3);
  margin-bottom: 12px;
}
.info__list { display: flex; flex-direction: column; gap: 2px; }
.info__row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px dashed var(--line-soft);
}
.info__row:last-child { border-bottom: none; }
.info__row dt { font-size: 13px; color: var(--ink-2); flex-shrink: 0; }
.info__row dd {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.info__row--full { flex-direction: column; align-items: flex-start; gap: 4px; }
.info__row--full dd { text-align: left; }
.info__badge {
  display: inline-block;
  font-family: var(--font-ui);
  font-size: 12px; font-weight: 600;
  padding: 3px 10px; border-radius: 999px;
}

/* ---------- Vùng vẽ ---------- */
.stage {
  position: relative;
  flex: 1;
  min-width: 0;
  background: var(--surface);
}
#chart { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }

.stage__tools {
  position: absolute;
  right: 16px; bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px;
  box-shadow: var(--shadow);
}
.stage__tools .tool { border: none; width: 34px; height: 34px; }
.stage__tools .tool:hover { background: var(--paper); }

.stage__hint {
  position: absolute;
  left: 16px; bottom: 16px;
  font-size: 12px;
  color: var(--ink-3);
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(4px);
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  pointer-events: none;
}

.scrim {
  position: fixed; inset: 0;
  background: rgba(20,30,45,.4);
  z-index: 40;
  opacity: 0;
  transition: opacity .25s;
}
.scrim.show { opacity: 1; }

/* ============================================================
   Responsive — bảng điều khiển trượt lên từ dưới trên mobile
   ============================================================ */
@media (max-width: 780px) {
  .icon-btn.topbar__toggle { display: inline-grid; }
  .readout { display: none; }

  .panel {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    width: auto;
    max-height: 82vh;
    border-right: none;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -8px 30px rgba(20,30,45,.14);
    transform: translateY(calc(100% + 20px));
    transition: transform .3s cubic-bezier(.4,0,.2,1);
    z-index: 50;
  }
  .panel.open { transform: translateY(0); }

  .panel__handle {
    display: block;
    width: 40px; height: 4px;
    background: var(--grid-bold);
    border-radius: 999px;
    margin: 10px auto 0;
  }
  .panel__head { padding: 8px 20px 6px; }
  .panel__head h2 { font-size: 15px; text-transform: none; letter-spacing: 0; color: var(--ink); }
  .panel__close { display: inline-grid; }
  .panel__body { padding-bottom: max(24px, env(safe-area-inset-bottom)); }

  .stage__hint { display: none; }
}

@media (max-width: 420px) {
  .topbar { gap: 10px; padding: 0 12px; }
}

@media (prefers-reduced-motion: reduce) {
  .panel, .scrim, .icon-btn, .btn { transition: none; }
}