/* ════════════════════════════════════════
   LAYOUT — khung app, sidebar, topbar, nav
   Mobile mặc định, PC override ở cuối file
════════════════════════════════════════ */

/* ── App shell ─────────────────────────── */
/* Mobile: flex cột dọc, giới hạn 480px giữa màn hình */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  background: var(--bg);
  position: relative;
}

/* Sidebar ẩn hoàn toàn trên mobile */
#sidebar { display: none; }

/* ── Topbar ─────────────────────────────── */
#topbar {
  background: var(--surface);
  height: var(--top-h);
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 8px rgba(0,0,0,0.06);
  flex-shrink: 0;
}
.topbar-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 100%;
  padding: 0 12px;
}
#page-title {
  color: var(--text);
  font-size: 1.1rem;
  font-weight: 800;
  flex: 1;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* Nút icon tròn trên topbar (back, settings, search) */
.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-muted);
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.12s, color 0.12s, transform 0.12s;
  flex-shrink: 0;
}
.btn-icon:hover  { background: rgba(0,0,0,0.06); color: var(--text); }
.btn-icon:active { transform: scale(0.93); background: rgba(0,0,0,0.1); }
.btn-icon.hidden { visibility: hidden; pointer-events: none; }

/* Avatar tròn trên topbar (kiểu Gmail/YouTube) */
.topbar-avatar-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 2px solid var(--purple-light);
  background: var(--purple-light);
  font-size: 1.25rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: border-color 0.2s, box-shadow 0.2s;
  flex-shrink: 0;
  padding: 0;
}
.topbar-avatar-btn:hover {
  border-color: var(--purple);
  box-shadow: 0 0 0 3px rgba(14,165,233,0.15);
}

/* Logo chỉ hiện trên PC */
.tb-logo { display: none; }

/* ── 3-zone mobile (left chiếm hết, center/right auto) ── */
.tb-left   { display: flex; align-items: center; gap: 6px; flex: 1; padding: 0 0 0 12px; min-width: 0; }
.tb-center { display: flex; align-items: center; }
.tb-right  { display: flex; align-items: center; gap: 6px; padding: 0 12px 0 0; }
.tb-settings-btn svg { display: block; }

/* ── Search bar (topbar) ─────────────────── */
#topbar-search-wrap {
  display: none; /* hidden on mobile by default */
  position: relative;
  flex: 1;
  align-items: center;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 50px;
  padding: 0 12px;
  height: 36px;
  gap: 7px;
  min-width: 0;
}
#topbar-search-wrap:focus-within {
  border-color: var(--purple);
  box-shadow: 0 0 0 3px rgba(14,165,233,0.1);
}
.tsw-icon { color: var(--text-muted); font-size: 0.85rem; flex-shrink: 0; line-height: 1; }
#topbar-search-input {
  flex: 1; border: none; background: transparent; outline: none;
  font-size: 0.88rem; color: var(--text); font-family: inherit;
  min-width: 0;
}
#topbar-search-input::placeholder { color: var(--text-muted); }
.tsw-clear {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 1.1rem; padding: 0; line-height: 1;
  flex-shrink: 0; display: flex; align-items: center;
}
.tsw-clear:hover { color: var(--text); }
.tsw-clear.hidden { display: none; }

/* Search dropdown */
.topbar-search-dd {
  position: absolute;
  top: calc(100% + 8px);
  left: -12px; right: -12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  z-index: 300;
  max-height: 360px;
  overflow-y: auto;
}
.topbar-search-dd.hidden { display: none; }
.tsd-section {
  font-size: 0.68rem; font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.5px;
  padding: 10px 14px 4px;
}
.tsd-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 14px; width: 100%;
  border: none; background: none; cursor: pointer;
  text-align: left; font-family: inherit;
  transition: background 0.15s;
}
.tsd-item:hover  { background: var(--bg); }
.tsd-icon  { font-size: 1rem; flex-shrink: 0; }
.tsd-name  { flex: 1; font-size: 0.88rem; color: var(--text); font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tsd-name mark { background: rgba(14,165,233,0.15); color: var(--purple); border-radius: 2px; padding: 0 1px; font-style: normal; }
.tsd-meta  { font-size: 0.75rem; color: var(--text-muted); flex-shrink: 0; max-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tsd-empty { padding: 20px 14px; color: var(--text-muted); font-size: 0.88rem; text-align: center; }

/* Mobile: search active */
#topbar.m-search #page-title        { display: none; }
#topbar.m-search #btn-search-toggle { display: none; }
#topbar.m-search #btn-settings      { display: none; }
#topbar.m-search .tb-left           { flex: 0; padding: 0 0 0 8px; }
#topbar.m-search .tb-center         { flex: 1; padding: 0 8px; }
#topbar.m-search .tb-right          { display: none; }
#topbar.m-search #topbar-search-wrap { display: flex; }

/* ── Main ───────────────────────────────── */
/* Vùng cuộn nội dung chính */
#main {
  --main-px: 16px; /* dùng để lib-page thoát ra bằng negative margin */
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px var(--main-px) calc(var(--nav-h) + 12px);
  scroll-behavior: smooth;
}

/* ── Bottom nav (mobile only) ───────────── */
#bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: var(--nav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
  box-shadow: 0 -2px 16px rgba(0,0,0,0.06);
}
.nav-item {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 3px;
  border: none; background: none;
  cursor: pointer;
  font-size: 0.62rem;
  color: var(--text-muted);
  font-weight: 500;
  transition: color 0.18s ease, transform 0.12s ease;
  padding: 6px 0 4px;
  font-family: inherit;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
}
@media (hover: hover) {
  .nav-item:hover { background: rgba(0,0,0,0.05); border-radius: 10px; }
}
.nav-item:active   { transform: scale(0.92); }
.nav-item.active   { color: var(--purple); font-weight: 700; }

/* Icon: inactive = outline stroke, active = filled solid (YouTube style) */
.nav-icon { display: flex; align-items: center; justify-content: center; }
.nav-icon svg,
.nav-icon svg path,
.nav-icon svg circle,
.nav-icon svg polygon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: fill 0.15s, stroke 0.15s;
}
.nav-icon svg { width: 22px; height: 22px; stroke: none; fill: none; }
.nav-item.active .nav-icon svg path,
.nav-item.active .nav-icon svg circle,
.nav-item.active .nav-icon svg polygon {
  fill: currentColor;
  stroke: none;
}

/* ── Trang ──────────────────────────────── */
/* Ẩn mặc định, hiện khi có .active */
.page        { display: none; animation: fadeIn 0.2s ease; }
.page.active { display: block; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* ── Toast thông báo ────────────────────── */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + 16px);
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: #1E293B;
  color: #fff;
  padding: 10px 22px;
  border-radius: 50px;
  font-size: 0.88rem;
  font-weight: 600;
  z-index: 300;
  white-space: nowrap;
  animation: toastIn 0.25s ease;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ── Scrollbar ──────────────────────────── */
#main::-webkit-scrollbar,
.modal::-webkit-scrollbar        { width: 4px; }
#main::-webkit-scrollbar-track,
.modal::-webkit-scrollbar-track  { background: transparent; }
#main::-webkit-scrollbar-thumb,
.modal::-webkit-scrollbar-thumb  { background: var(--border); border-radius: 2px; }


/* ════════════════════════════════════════
   Tablet ≥ 640px — sidebar icon-only
════════════════════════════════════════ */
@media (min-width: 640px) {

  /* App chuyển sang layout ngang */
  #app { max-width: none; height: 100vh; flex-direction: row; overflow: hidden; }

  /* Sidebar icon-only — chỉ hiện icon, ẩn label */
  #sidebar {
    display: flex;
    flex-direction: column;
    width: var(--sb-w-icon);
    background: var(--surface);
    border-right: 1px solid var(--border);
    flex-shrink: 0;
    position: fixed;
    left: 0; top: var(--top-h); bottom: 0;
    z-index: 100;
    overflow: hidden;
  }
  .sb-logo   { display: none; }
  .sb-footer { display: none; }

  .sb-nav {
    flex: 1; display: flex; flex-direction: column;
    gap: 2px; padding: 12px 8px;
    align-items: center;
  }
  .sb-item {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 44px;
    border: none; background: transparent;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer; font-family: inherit;
    transition: all 0.18s;
    position: relative;
    padding: 0;
  }
  /* Ẩn text label, chỉ giữ icon */
  .sb-item > span:not(.sb-icon):not(.sb-badge) { display: none; }
  .sb-item:hover  { background: var(--bg); color: var(--text); }
  .sb-item.active { background: var(--purple-light); color: var(--purple); }
  .sb-icon     { width: 20px; height: 20px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
  .sb-icon svg { width: 20px; height: 20px; stroke: currentColor; }

  /* Badge dot nhỏ ở góc trên icon */
  .sb-badge {
    position: absolute; top: 6px; right: 6px;
    width: 8px; height: 8px; min-width: unset;
    background: #EF4444; border-radius: 50%;
    font-size: 0; padding: 0;
  }
  .sb-badge.hidden { display: none; }

  /* Topbar fixed */
  #topbar {
    position: fixed; left: 0; top: 0;
    width: 100%; height: var(--top-h);
    background: var(--surface); border-bottom: 1px solid var(--border);
    box-shadow: 0 1px 8px rgba(0,0,0,0.06); z-index: 200;
  }
  .topbar-inner { display: flex; align-items: center; height: 100%; padding: 0; gap: 0; }

  /* Left zone — cùng rộng sidebar icon */
  .tb-left {
    width: var(--sb-w-icon); flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    padding: 0;
  }
  .tb-center {
    flex: 1; display: flex; align-items: center;
    padding: 0 12px; min-width: 0;
  }
  .tb-right {
    flex-shrink: 0; display: flex; align-items: center;
    padding: 0 12px; gap: 8px;
  }

  /* Logo: chỉ hiện mark, ẩn text trên tablet */
  .tb-logo { display: flex; align-items: center; cursor: pointer; flex-shrink: 0; }
  .tb-logo-mark {
    width: 28px; height: 28px;
    background: linear-gradient(135deg, #0EA5E9, #7DD3FC);
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.9rem; font-weight: 900; color: #fff; flex-shrink: 0;
  }
  .tb-logo-text { display: none; }

  #page-title        { display: none; }
  #btn-search-toggle { display: none; }
  #btn-settings      { display: flex; background: none; color: var(--text-muted); }
  #btn-settings:hover { background: var(--bg); color: var(--text); }

  /* Search bar hiện trên tablet */
  #topbar-search-wrap { display: flex; width: 100%; max-width: 700px; }

  /* Main: lùi sang phải theo icon sidebar */
  #main {
    --main-px: clamp(16px, 3vw, 40px);
    margin-left: var(--sb-w-icon);
    flex: 1; overflow-y: auto;
    padding: calc(var(--top-h) + 20px) var(--main-px) 32px;
    height: 100vh; max-width: none;
  }

  /* Bottom nav ẩn từ tablet */
  #bottom-nav { display: none; }
  .toast { bottom: 20px; }

  .page { animation: fadeInPC 0.18s ease; }
  @keyframes fadeInPC {
    from { opacity: 0; }
    to   { opacity: 1; }
  }
}

/* ════════════════════════════════════════
   Desktop ≥ 1024px — sidebar đầy đủ + collapsible
════════════════════════════════════════ */
@media (min-width: 1024px) {

  /* Sidebar đầy đủ với labels + transition */
  #sidebar {
    width: var(--sb-w);
    transition: width 0.24s ease;
    display: flex; flex-direction: column;
    overflow: hidden;
  }

  .sb-logo {
    display: flex; align-items: center; gap: 10px;
    padding: 16px 14px;
    border-bottom: 1px solid var(--border);
    overflow: hidden; flex-shrink: 0;
  }
  .sb-logo-text {
    max-width: 160px; opacity: 1;
    transition: max-width 0.24s ease, opacity 0.18s ease, margin-left 0.22s ease;
    font-size: 0.95rem; font-weight: 800; color: var(--text);
    white-space: nowrap; overflow: hidden;
  }

  .sb-nav { padding: 12px; align-items: stretch; }
  .sb-item {
    width: auto; height: auto;
    display: flex; align-items: center; justify-content: flex-start;
    gap: 0;
    padding: 9px 12px;
    font-size: 0.9rem; font-weight: 600;
    text-align: left;
    transition: padding 0.22s ease;
    position: relative;
  }
  .sb-icon { width: 18px; height: 18px; flex-shrink: 0; }
  .sb-icon svg { width: 18px; height: 18px; }

  /* Label với transition (không dùng display:none) */
  .sb-label {
    display: inline-block; overflow: hidden; white-space: nowrap;
    max-width: 150px; opacity: 1; margin-left: 10px;
    transition: max-width 0.24s ease, opacity 0.18s ease, margin-left 0.22s ease;
  }

  /* Badge dạng số như cũ */
  .sb-badge {
    position: static;
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 18px; width: auto; height: 18px;
    background: #EF4444; color: #fff;
    border-radius: 50px; font-size: 0.65rem; font-weight: 700;
    padding: 0 5px; margin-left: 8px; line-height: 1;
  }

  /* Footer với transition */
  .sb-footer {
    display: flex; flex-direction: column; gap: 6px;
    padding: 10px 14px;
    border-top: 1px solid var(--border);
    transition: opacity 0.18s ease, max-height 0.24s ease, padding 0.24s ease;
    overflow: hidden; max-height: 120px;
  }

  /* Toggle button */
  .sb-toggle-btn {
    display: flex; align-items: center; justify-content: center;
    width: 100%; padding: 10px;
    border: none; border-top: 1px solid var(--border);
    background: none; cursor: pointer; color: var(--text-muted);
    transition: background 0.18s, color 0.18s;
    flex-shrink: 0;
  }
  .sb-toggle-btn:hover { background: var(--bg); color: var(--text); }
  .sb-toggle-btn svg {
    width: 16px; height: 16px;
    transition: transform 0.24s ease;
  }

  /* Left zone — khớp với sidebar đầy */
  .tb-left {
    width: var(--sb-w);
    display: flex; align-items: center; gap: 8px;
    justify-content: flex-start; padding: 0 16px;
    transition: width 0.24s ease;
  }
  .tb-right { width: var(--sb-w); justify-content: flex-end; padding: 0 20px; }

  /* Logo hiện text trên desktop */
  .tb-logo { gap: 10px; }
  .tb-logo-mark { width: 32px; height: 32px; border-radius: 9px; font-size: 1rem; }
  .tb-logo-text {
    display: block;
    color: var(--text); font-size: 1rem; font-weight: 800;
    letter-spacing: 0.2px; white-space: nowrap;
  }

  /* Main: lùi theo sidebar đầy + transition */
  #main {
    --main-px: clamp(20px, 2.5vw, 48px);
    margin-left: var(--sb-w);
    padding: calc(var(--top-h) + 24px) var(--main-px) 40px;
    transition: margin-left 0.24s ease;
  }

  .toast { bottom: 24px; }
}

/* ════════════════════════════════════════
   Large Desktop ≥ 1200px — sidebar rộng hơn
════════════════════════════════════════ */
@media (min-width: 1200px) {
  :root { --sb-w: 240px; }
}

/* ════════════════════════════════════════
   Shell states — collapsed / settings
════════════════════════════════════════ */
@media (min-width: 1024px) {
  html[data-shell="collapsed"] { --sb-w: 72px; }
  html[data-shell="collapsed"] .sb-label { max-width: 0; opacity: 0; margin-left: 0; }
  html[data-shell="collapsed"] .sb-logo-text { max-width: 0; opacity: 0; }
  html[data-shell="collapsed"] .sb-footer { opacity: 0; pointer-events: none; padding-top: 0; padding-bottom: 0; max-height: 0; }
  html[data-shell="collapsed"] .sb-nav { padding: 12px 4px; }
  html[data-shell="collapsed"] .sb-item { justify-content: center; padding: 9px 0; }
  html[data-shell="collapsed"] .sb-logo { justify-content: center; padding: 16px 0; }
  html[data-shell="collapsed"] .sb-badge {
    position: absolute; top: 4px; right: 4px;
    width: 8px; height: 8px; min-width: unset;
    font-size: 0; padding: 0; margin-left: 0; border-radius: 50%;
  }
  html[data-shell="collapsed"] .sb-toggle-btn svg { transform: rotate(180deg); }
}

@media (min-width: 640px) {
  html[data-shell="settings"] #sidebar { width: 0 !important; overflow: hidden; }
  html[data-shell="settings"] #main { margin-left: 0 !important; padding-bottom: 0 !important; }
  html[data-shell="settings"] .tb-left { width: 0 !important; padding: 0 !important; }
}

/* backward compat */
#app.settings-mode #sidebar { display: none !important; }
#app.settings-mode #bottom-nav { display: none !important; }
#app.settings-mode #main { margin-left: 0 !important; padding-bottom: 0 !important; }

/* Fluid content container */
.page-wrap { width: 100%; max-width: 1600px; margin-inline: auto; }

/* ════════════════════════════════════════
   FOCUS MODE — chế độ tập trung học từ
════════════════════════════════════════ */
#app.focus-mode #sidebar,
#app.focus-mode #topbar,
#app.focus-mode #bottom-nav { display: none !important; }

#app.focus-mode #main {
  margin-left: 0 !important;
  padding: 0 !important;
  height: 100vh;
  overflow: hidden;
  max-width: none;
}

#app.focus-mode .page.active {
  display: flex !important;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* Header tối giản trong focus mode */
.focus-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.focus-exit-btn {
  height: 34px;
  padding: 0 12px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 50px;
  display: flex; align-items: center; justify-content: center; gap: 4px;
  cursor: pointer; font-size: 0.82rem; font-weight: 600; line-height: 1;
  color: var(--text-muted); font-family: inherit;
  transition: all 0.18s; flex-shrink: 0;
}
.focus-exit-btn:hover { background: var(--purple-light); color: var(--purple); border-color: var(--purple); }
.focus-deck-name {
  font-size: 0.9rem; font-weight: 700; color: var(--text);
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Vùng nội dung cuộn bên trong focus mode */
.focus-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 32px;
}

@media (min-width: 640px) {
  .focus-header  { padding: 14px clamp(20px, 3vw, 40px); }
  .focus-content { padding: clamp(20px, 3vw, 32px) clamp(20px, 3vw, 40px) 40px; }
}
