/* CSS RESET & VARIABLES */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-main: #05070a;
  --bg-panel: #0d1117;
  --bg-panel-sub: #090d14;
  --bg-input: #090d13;
  --border-color: #1e2638;
  --border-focus: #38bdf8;
  
  --text-main: #cbd5e1;
  --text-muted: #64748b;
  --text-bright: #f8fafc;
  
  --btn-bg: #0284c7;
  --btn-hover: #0369a1;
  --btn-border: #38bdf8;

  /* Nút Nguy hiểm/Xóa: Xám than viền bạc (Tuyệt đối không dùng đỏ) */
  --btn-danger-bg: #1e2430;
  --btn-danger-color: #94a3b8;
  --btn-danger-border: #334155;
  --btn-danger-hover-border: #38bdf8;
  --btn-danger-hover-color: #cbd5e1;

  /* Translation 4-tier token colors */
  --word-name: #7dd3fc;           /* Tầng 1: Names (Xanh ngọc băng) */
  --word-rule: #60a5fa;           /* Tầng 2: Rules (Xanh lam dịu) */
  --word-vp: #f8fafc;             /* Tầng 3: VietPhrase (Bạc sáng) */
  --word-raw: #94a3b8;            /* Tầng 4: Raw (Xám bạc gạch chân) */
  --word-raw-hover-bg: #0c4a6e;
  --word-raw-hover-text: #e0f2fe;
  
  --word-active-bg: #0e2b44;
  --word-active-border: #38bdf8;
}

body {
  background-color: var(--bg-main);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-size: 13.5px;
  line-height: 1.5;
}

/* SCROLLBAR */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: var(--bg-main);
}
::-webkit-scrollbar-thumb {
  background: #1e293b;
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: #334155;
}

/* TOP TOOLBAR */
#toolbar {
  height: 48px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  flex-shrink: 0;
  gap: 10px;
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
}

.app-title {
  font-size: 14.5px;
  font-weight: 700;
  color: #38bdf8;
  letter-spacing: 0.5px;
  margin-right: 4px;
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

/* BỘ CHUYỂN CỜ 3 NGÔN NGỮ */
.lang-switch-group {
  display: flex;
  align-items: center;
  background: #090d14;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 2px;
  gap: 2px;
  margin-right: 4px;
}

.btn-lang {
  background: transparent;
  border: 1px solid transparent;
  color: #94a3b8;
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 3px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.btn-lang:hover {
  color: var(--text-bright);
  border-color: #334155;
}

.btn-lang.active {
  border: 1px solid #38bdf8;
  background: #0c4a6e;
  color: #f8fafc;
  font-weight: 600;
}

button {
  background: var(--btn-bg);
  color: var(--text-bright);
  border: 1px solid var(--btn-border);
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: all 0.15s ease;
  white-space: nowrap;
}

button:hover {
  background: var(--btn-hover);
  border-color: #7dd3fc;
}

.btn-secondary {
  background: #111827;
  border: 1px solid var(--border-color);
  color: #cbd5e1;
}
.btn-secondary:hover {
  background: #1e293b;
  border-color: #475569;
  color: var(--text-bright);
}

.btn-danger {
  background: var(--btn-danger-bg);
  color: var(--btn-danger-color);
  border: 1px solid var(--btn-danger-border);
}
.btn-danger:hover {
  border-color: var(--btn-danger-hover-border);
  color: var(--btn-danger-hover-color);
  background: #172033;
}

.btn-toggle {
  background: #090d14;
  border: 1px solid var(--border-color);
  color: #64748b;
}
.btn-toggle.active {
  background: #082f49;
  border-color: #38bdf8;
  color: #38bdf8;
  font-weight: 600;
}

.stats-badge {
  font-size: 11.5px;
  color: var(--text-muted);
  border: 1px solid var(--border-color);
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--bg-input);
  white-space: nowrap;
}
.stats-badge span {
  color: #38bdf8;
  font-weight: 600;
}

/* MAIN CONTAINER */
#mainLayout {
  flex: 1;
  display: flex;
  height: calc(100vh - 48px);
  overflow: hidden;
}

/* LEFT COLUMN (35%) */
#leftCol {
  width: 35%;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
}

.panel-box {
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--border-color);
}
.panel-box:last-child {
  border-bottom: none;
}

.panel-header {
  padding: 5px 12px;
  font-size: 11.5px;
  font-weight: 600;
  color: #94a3b8;
  background: var(--bg-panel-sub);
  border-bottom: 1px solid var(--border-color);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#rawInputWrapper {
  height: 42%;
}
#rawInput {
  width: 100%;
  height: 100%;
  background: var(--bg-input);
  border: none;
  outline: none;
  color: var(--text-main);
  padding: 10px 12px;
  resize: none;
  font-family: inherit;
  font-size: 13.5px;
  line-height: 1.6;
}
#rawInput:focus {
  background: #0b1118;
}

#hanvietWrapper {
  height: 28%;
}
#hanvietBox {
  flex: 1;
  padding: 10px 12px;
  overflow-y: auto;
  color: #94a3b8;
  font-size: 13px;
  line-height: 1.6;
  background: var(--bg-input);
  white-space: pre-wrap;
  word-break: break-word;
}

#detailWrapper {
  height: 30%;
}
#detailBox {
  flex: 1;
  padding: 10px 12px;
  overflow-y: auto;
  background: var(--bg-input);
  font-size: 13px;
  line-height: 1.6;
}

.detail-item {
  margin-bottom: 5px;
}
.detail-label {
  color: #64748b;
  font-size: 11px;
  text-transform: uppercase;
  display: block;
}
.detail-val {
  color: var(--text-bright);
  font-weight: 500;
}
.detail-val.cyan {
  color: #7dd3fc;
}
.detail-val.blue {
  color: #60a5fa;
}
.detail-val.sub {
  color: #94a3b8;
}

/* RIGHT COLUMN (65%) */
#rightCol {
  width: 65%;
  display: flex;
  flex-direction: column;
  background: var(--bg-main);
}

.tabs-bar {
  display: flex;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  height: 34px;
}

.tab-item {
  padding: 0 16px;
  height: 100%;
  display: flex;
  align-items: center;
  font-size: 12px;
  font-weight: 600;
  color: #38bdf8;
  background: var(--bg-main);
  border-right: 1px solid var(--border-color);
  border-top: 2px solid #38bdf8;
  cursor: default;
}

#outputArea {
  flex: 1;
  padding: 18px 24px;
  overflow-y: auto;
  line-height: 1.85;
  font-size: 15px;
  white-space: normal;
  word-break: break-word;
}

/* 4-TIER WORD TOKENS */
.qt-word {
  display: inline;
  padding: 1px 2px;
  border-radius: 2px;
  cursor: pointer;
  user-select: text;
  transition: background 0.1s ease, color 0.1s ease;
}

/* Tầng 1: Names */
.qt-name {
  color: var(--word-name);
  font-weight: 500;
}
.qt-name:hover {
  background: #0c2d48;
}

/* Tầng 2: Rules (Luật nhân xưng) */
.qt-rule {
  color: var(--word-rule);
  font-weight: 500;
}
.qt-rule:hover {
  background: #0f274a;
}

/* Tầng 3: VietPhrase */
.qt-vp {
  color: var(--word-vp);
}
.qt-vp:hover {
  background: #1e293b;
}

/* Tầng 4: Raw */
.qt-raw {
  color: var(--word-raw);
  border-bottom: 1px dashed #94a3b8;
}
.qt-raw:hover {
  color: var(--word-raw-hover-text);
  background: var(--word-raw-hover-bg);
}

.qt-active {
  background: var(--word-active-bg) !important;
  outline: 1px solid var(--word-active-border);
}

.qt-paragraph {
  margin-bottom: 14px;
}

/* MODALS */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(5, 7, 10, 0.82);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(2px);
}

.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: 0 15px 35px rgba(0,0,0,0.8);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 12px 18px;
  background: var(--bg-panel-sub);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #38bdf8;
  font-weight: 600;
  font-size: 14px;
}

.modal-body {
  padding: 16px;
}

.modal-footer {
  padding: 12px 18px;
  background: var(--bg-panel-sub);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
}

/* Đảm bảo khung Sửa từ luôn nổi lên trên Bảng Quản lý */
#modalAddWord {
  z-index: 2100 !important;
}

#modalAddWord .modal-box {
  width: 440px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
}
.form-group label {
  font-size: 12px;
  color: #94a3b8;
}
.form-group input, .form-group select {
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 8px 10px;
  color: var(--text-bright);
  outline: none;
  font-size: 13px;
}
.form-group input:focus, .form-group select:focus {
  border-color: #38bdf8;
}
.form-group input[readonly] {
  background: #0b1118;
  color: #7dd3fc;
  border-color: #1e293b;
  cursor: not-allowed;
}

#modalManageDict .modal-box {
  width: 900px;
  max-width: 95vw;
  height: 82vh;
}

.manage-toolbar {
  padding: 10px 18px;
  background: #0b1017;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.manage-search-box {
  flex: 1;
  position: relative;
}
.manage-search-box input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 7px 12px;
  color: var(--text-bright);
  outline: none;
  font-size: 13px;
}
.manage-search-box input:focus {
  border-color: #38bdf8;
}

.manage-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-panel);
}

/* Chế độ Bảng */
#dictTableView {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.table-container {
  flex: 1;
  overflow-y: auto;
}
table.dict-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 13px;
}
table.dict-table th {
  position: sticky;
  top: 0;
  background: #090d14;
  padding: 8px 12px;
  color: #94a3b8;
  border-bottom: 1px solid var(--border-color);
  font-weight: 600;
  z-index: 10;
}
table.dict-table td {
  padding: 7px 12px;
  border-bottom: 1px solid #161f30;
  color: var(--text-main);
}
table.dict-table tr:hover td {
  background: #0e1624;
}
.col-raw { width: 22%; font-family: monospace; font-size: 14px; color: #7dd3fc; }
.col-val { width: 38%; color: #f8fafc; }
.col-hv  { width: 25%; color: #94a3b8; }
.col-act { width: 15%; text-align: right; }

/* Chế độ Notepad */
#dictNotepadView {
  flex: 1;
  display: none;
  flex-direction: column;
  padding: 12px 18px;
  background: var(--bg-input);
}
#notepadTextarea {
  flex: 1;
  width: 100%;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  color: #cbd5e1;
  padding: 12px;
  font-family: Consolas, Monaco, "Courier New", monospace;
  font-size: 13.5px;
  line-height: 1.6;
  resize: none;
  outline: none;
}
#notepadTextarea:focus {
  border-color: #38bdf8;
}
.notepad-hint {
  margin-top: 8px;
  font-size: 12px;
  color: #64748b;
}

/* Phân trang */
.pagination-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 18px;
  background: var(--bg-panel-sub);
  border-top: 1px solid var(--border-color);
  font-size: 12px;
  color: #94a3b8;
}
.pagination-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Toast */
#notifyToast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: #082f49;
  color: #e0f2fe;
  border: 1px solid #38bdf8;
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 13px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.5);
  display: none;
  z-index: 2000;
}

/* ================= LIÊN KẾT ĐỒNG BỘ 3 KHUNG (LINKED TOKENS) ================= */

/* Màu bôi đen chuẩn Dark Mode cho khung Textarea Văn bản thô */
#rawInput::selection {
  background: #0c4a6e;
  color: #e0f2fe;
}

/* Token chữ trong khung Phiên âm (Hán Việt / Romaja) */
.qt-phonetic-word {
  display: inline;
  padding: 1px 2px;
  border-radius: 2px;
  cursor: pointer;
  user-select: text;
  transition: background 0.1s ease, color 0.1s ease;
  color: #94a3b8;
}

.qt-phonetic-word:hover {
  background: #1e293b;
  color: #f8fafc;
}

/* Trạng thái được chọn đồng bộ (Active) của Phiên âm */
.qt-phonetic-word.qt-active {
  background: var(--word-active-bg) !important;
  outline: 1px solid var(--word-active-border);
  color: #7dd3fc !important;
}

/* Hiệu ứng rê chuột đồng bộ giữa VP và Phiên âm */
.qt-hover-sync {
  background: #0c2d48 !important;
  color: #e0f2fe !important;
}

/* ================= QUICK TRANSLATOR POPUP CONTEXT MENU ================= */
.qt-context-menu {
  position: fixed;
  display: none;
  z-index: 3000;
  width: 240px;
  max-width: 90vw;
  background: #0d1117;
  border: 1px solid #1e2638;
  border-radius: 5px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8), 0 0 0 1px #161f30;
  overflow: hidden;
  font-size: 13px;
  user-select: none;
}

/* Header hiển thị chữ gốc và phiên âm */
.context-menu-header {
  padding: 8px 12px;
  background: #090d14;
  border-bottom: 1px solid #1e2638;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11.5px;
}
.context-menu-header #contextRaw {
  color: #7dd3fc;
  font-weight: 600;
  font-family: monospace;
  font-size: 13px;
}
.context-menu-header #contextPhonetic {
  color: #94a3b8;
  font-style: italic;
}

/* Danh sách các nghĩa có sẵn */
.context-meaning-list {
  max-height: 220px;
  overflow-y: auto;
  background: #0d1117;
}

.context-meaning-item {
  padding: 6px 12px;
  color: #cbd5e1;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: all 0.12s ease;
  border-bottom: 1px solid #141b27;
}

.context-meaning-item:hover {
  background: #0c2d48;
  color: #e0f2fe;
}

/* Nghĩa ưu tiên số 1 (đầu danh sách) */
.context-meaning-item.priority {
  color: #38bdf8;
  font-weight: 600;
  background: #0a1b2d;
}
.context-meaning-item.priority::after {
  content: "✔";
  font-size: 11px;
  color: #38bdf8;
}

/* Thông báo khi từ chưa có nghĩa */
.context-empty-hint {
  padding: 12px;
  color: #64748b;
  font-size: 12px;
  text-align: center;
  font-style: italic;
}

/* Ô nhập nghĩa mới ở cuối menu */
.context-input-wrapper {
  padding: 8px 10px;
  background: #090d14;
  border-top: 1px solid #1e2638;
}
.context-input-wrapper input {
  width: 100%;
  background: #05070a;
  border: 1px solid #1e2638;
  border-radius: 3px;
  padding: 5px 8px;
  color: #f8fafc;
  font-size: 12.5px;
  outline: none;
}
.context-input-wrapper input:focus {
  border-color: #38bdf8;
  background: #090e17;
}

/* Nút bấm Update VietPhrase */
.context-actions {
  padding: 6px 10px;
  background: #0b111a;
  border-top: 1px solid #1e2638;
}
.btn-context-update {
  width: 100%;
  padding: 5px 0;
  background: #0284c7;
  color: #f8fafc;
  border: 1px solid #38bdf8;
  border-radius: 3px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}
.btn-context-update:hover {
  background: #0369a1;
  border-color: #7dd3fc;
}

/* ================= RIGHT-CLICK CONTEXT MENU (BÔI ĐEN + CHUỘT PHẢI) ================= */
.qt-right-context-menu {
  position: fixed;
  display: none;
  z-index: 3500;
  width: 175px;
  background: #0d1117;
  border: 1px solid #1e2638;
  border-radius: 4px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.85), 0 0 0 1px #161f30;
  padding: 4px 0;
  user-select: none;
}

.qt-right-menu-item {
  padding: 7px 14px;
  font-size: 13px;
  color: #cbd5e1;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: all 0.12s ease;
}

.qt-right-menu-item:hover {
  background: #0c2d48;
  color: #38bdf8;
}

/* Đường phân cách mờ */
.qt-right-menu-item:not(:last-child) {
  border-bottom: 1px solid #141b27;
}

/* ================= POPUP XÁC NHẬN XÓA (THEME DARK + NÚT ĐỎ) ================= */
.confirm-modal-box {
  width: 390px !important;
  max-width: 90vw;
  background: #0d1117;
  border: 1px solid #1e2638;
  border-radius: 8px;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.9), 0 0 0 1px #161f30;
  overflow: hidden;
  animation: confirmPopIn 0.15s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes confirmPopIn {
  from {
    opacity: 0;
    transform: scale(0.94);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.confirm-modal-header {
  padding: 12px 18px;
  background: #090d14;
  border-bottom: 1px solid #1e2638;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.confirm-title {
  font-size: 14px;
  font-weight: 600;
  color: #cbd5e1;
  letter-spacing: 0.3px;
}

.confirm-modal-body {
  padding: 20px 18px;
  display: flex;
  align-items: flex-start;
  gap: 15px;
  background: #0d1117;
}

/* Icon thùng rác cảnh báo màu đỏ */
.confirm-icon-wrap {
  flex-shrink: 0;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: #25090b;
  border: 1px solid #7f1d1d;
  color: #ef4444;
  display: flex;
  align-items: center;
  justify-content: center;
}

.confirm-msg {
  font-size: 13.5px;
  line-height: 1.55;
  color: #94a3b8;
  word-break: break-word;
}

.confirm-msg strong {
  color: #f8fafc;
}

.confirm-modal-footer {
  padding: 12px 18px;
  background: #090d14;
  border-top: 1px solid #1e2638;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* NÚT XÓA MÀU ĐỎ NỔI BẬT */
.btn-modal-danger {
  background: #991b1b;
  border: 1px solid #ef4444;
  color: #ffffff;
  font-weight: 600;
  padding: 6px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12.5px;
  transition: all 0.15s ease;
}

.btn-modal-danger:hover {
  background: #dc2626;
  border-color: #f87171;
  box-shadow: 0 0 12px rgba(239, 68, 68, 0.45);
}

/* Cập nhật luôn nút [Xóa] trong bảng dữ liệu sang tone đỏ đậm */
.btn-danger {
  background: #220b0d !important;
  color: #fca5a5 !important;
  border: 1px solid #7f1d1d !important;
}

.btn-danger:hover {
  background: #991b1b !important;
  border-color: #ef4444 !important;
  color: #ffffff !important;
  box-shadow: 0 0 8px rgba(239, 68, 68, 0.35);
}