:root {
    --bg: #0b0b0c; --side: #111113; --surface: #18181b; --field: #0b0b0c; --raise: #232327;
    --ink: #f4f1f1; --muted: #a3999a; --line: #2f2a2b;
    --accent: #e11d2e; --accent-hover: #ff3b4a; --on-accent: #ffffff;
    --tally: #ff4d4d; --on-tally: #1a0000; --danger: #ff6b6b;
    --stage: #000000; --code-bg: #0a0a0b; --code-ink: #f1e9e9;
    color-scheme: dark;
  }
  :root[data-theme="light"] {
    --bg: #f6f2f2; --side: #fbf8f8; --surface: #ffffff; --field: #ffffff; --raise: #ece4e4;
    --ink: #170b0c; --muted: #6e5b5d; --line: #e2d6d7;
    --accent: #c8102e; --accent-hover: #a50d26; --on-accent: #ffffff;
    --tally: #ff4d4d; --on-tally: #1a0000; --danger: #c42b1c;
    color-scheme: light;
  }

  * { box-sizing: border-box; }
  [hidden] { display: none !important; }
  /* Mọi checkbox trong app đồng bộ màu với tab đang mở (--accent đổi theo data-mode) */
  input[type="checkbox"] { accent-color: var(--accent); }
  html, body { margin: 0; }
  body {
    font-family: "Be Vietnam Pro", "Segoe UI", system-ui, sans-serif;
    font-size: 15px;
    line-height: 1.55;
    background: var(--bg);
    color: var(--ink);
    height: 100vh;
    overflow: hidden;
    display: grid;
    grid-template-rows: auto 1fr;
  }
  ::selection { background: var(--accent); color: var(--on-accent); }
  :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

  /* ---------- Thanh trên ---------- */
  .top {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    padding: 14px 24px;
    border-bottom: 1px solid var(--line);
  }
  .brand {
    display: inline-flex; align-items: center; gap: 10px;
    font-weight: 800; font-size: 18px; letter-spacing: -0.01em;
    color: var(--ink); text-decoration: none; justify-self: start;
  }
  .mark { width: 26px; height: 26px; color: var(--accent); }
  .modes { display: inline-flex; background: var(--raise); border-radius: 12px; padding: 4px; }
  .mode { background: transparent; color: var(--muted); padding: 8px 24px; border-radius: 9px; }
  .mode:hover { color: var(--ink); }
  .mode.active { background: var(--ink); color: var(--bg); }
  .icon-btn {
    justify-self: end; width: 40px; height: 40px; padding: 0; border-radius: 10px;
    background: transparent; border: 1px solid var(--line); color: var(--ink);
  }
  .icon-btn:hover { border-color: var(--ink); }
  .ico { width: 18px; height: 18px; flex: none; }

  /* ---------- Bố cục ---------- */
  .shell { display: grid; grid-template-columns: 400px minmax(0, 1fr); min-height: 0; height: 100%; overflow: hidden; }
  .side {
    background: var(--side);
    border-right: 1px solid var(--line);
    padding: 32px 26px 36px;
    height: 100%; min-height: 0; overflow-y: auto;
  }
  .work { position: relative; padding: 32px 36px 56px; min-width: 0; height: 100%; min-height: 0; overflow-y: auto; }

  h1 { font-size: 28px; line-height: 1.15; font-weight: 800; letter-spacing: -0.02em; margin: 0 0 8px; text-wrap: balance; }
  .lead { color: var(--muted); margin: 0 0 26px; max-width: 34ch; }

  .form { display: flex; flex-direction: column; gap: 20px; }
  label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; }
  textarea, input[type=text], input[type=password], select {
    width: 100%; font: inherit; color: var(--ink);
    background: var(--field); border: 1px solid var(--line); border-radius: 12px;
    padding: 12px 14px; transition: border-color 0.15s, box-shadow 0.15s;
  }
  textarea { min-height: 168px; resize: vertical; line-height: 1.5; }
  textarea:focus, input:focus, select:focus {
    outline: none; border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
  }
  select {
    appearance: none; padding-right: 36px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none' stroke='%238b87b5' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M1 1.5l5 5 5-5'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 14px center;
  }
  .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .hint { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
  .chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
  .chip {
    padding: 6px 12px; font-size: 13px; font-weight: 500; border-radius: 999px;
    background: transparent; border: 1px solid var(--line); color: var(--muted);
  }
  .chip:hover { color: var(--ink); border-color: var(--ink); }

  /* ---------- Nút ---------- */
  button {
    font: inherit; font-weight: 600; border: 0; border-radius: 12px; padding: 11px 16px;
    cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
  }
  button:active:not(:disabled) { transform: translateY(1px); }
  button:disabled { opacity: 0.5; cursor: not-allowed; }
  .primary { background: var(--accent); color: var(--on-accent); }
  .primary:hover:not(:disabled) { background: var(--accent-hover); }
  .big { width: 100%; padding: 14px 18px; font-size: 16px; }
  .ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
  .ghost:hover:not(:disabled) { border-color: var(--ink); }
  .record { background: var(--tally); color: var(--on-tally); }
  .record:hover:not(:disabled) { filter: brightness(0.95); }
  .primary.loading::before {
    content: ""; width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent;
    border-radius: 50%; animation: spin 0.7s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  .status { min-height: 22px; font-size: 14px; color: var(--muted); margin-top: 14px; }
  .status.error { color: var(--danger); }

  /* ---------- Thanh tiến trình khi AI đang xử lý ---------- */
  .progress { position: absolute; left: 0; right: 0; top: 0; height: 3px; overflow: hidden; opacity: 0; transition: opacity 0.2s; }
  .progress::before {
    content: ""; position: absolute; inset: 0; width: 35%; background: var(--accent); border-radius: 2px;
    animation: slide 1.2s ease-in-out infinite;
  }
  .busy .progress { opacity: 1; }
  @keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(300%); } }

  /* ---------- Khung ngắm (video) ---------- */
  .stage {
    position: relative; background: var(--stage); border-radius: 20px;
    padding: 34px; min-height: 400px;
    display: flex; align-items: center; justify-content: center; overflow: hidden;
  }
  .vf { position: absolute; width: 22px; height: 22px; border: 0 solid var(--accent); opacity: 0.8; pointer-events: none; }
  .vf.tl { top: 16px; left: 16px; border-top-width: 2px; border-left-width: 2px; border-top-left-radius: 7px; }
  .vf.tr { top: 16px; right: 16px; border-top-width: 2px; border-right-width: 2px; border-top-right-radius: 7px; }
  .vf.bl { bottom: 16px; left: 16px; border-bottom-width: 2px; border-left-width: 2px; border-bottom-left-radius: 7px; }
  .vf.br { bottom: 16px; right: 16px; border-bottom-width: 2px; border-right-width: 2px; border-bottom-right-radius: 7px; }
  canvas { display: block; max-width: 100%; max-height: 62vh; width: auto; height: auto; border-radius: 6px; }
  .rec-badge {
    position: absolute; top: 14px; left: 50%; transform: translateX(-50%);
    display: inline-flex; align-items: center; gap: 8px;
    font-size: 12px; font-weight: 600; color: var(--tally);
  }
  .rec-badge i { width: 9px; height: 9px; border-radius: 50%; background: var(--tally); animation: pulse 1s ease-in-out infinite; }
  .stage:not(.recording) .rec-badge { display: none; }
  @keyframes pulse { 50% { opacity: 0.25; } }

  .empty { text-align: center; color: var(--muted); padding: 40px 24px; }
  .empty strong { display: block; font-size: 17px; color: var(--ink); margin-bottom: 4px; }
  .stage .empty { color: #a3999a; }
  .stage .empty strong { color: #f4f1f1; }
  .empty.plain {
    border: 1.5px dashed var(--line); border-radius: 20px; min-height: 400px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; text-align: center;
  }

  /* ---------- Timeline ---------- */
  .timeline { display: flex; gap: 4px; margin-top: 22px; height: 46px; position: relative; }
  .seg {
    flex: 1 1 0; min-width: 0; padding: 0 10px; border-radius: 10px;
    background: var(--raise); color: var(--muted); border: 1px solid transparent;
    font-size: 12px; font-weight: 500; justify-content: space-between; overflow: hidden; white-space: nowrap;
  }
  .seg b { font-weight: 800; font-size: 13px; }
  .seg:hover { border-color: var(--accent); color: var(--ink); }
  .seg.active { background: var(--accent); color: var(--on-accent); }
  .playhead {
    position: absolute; top: -6px; bottom: -6px; width: 3px; left: 0;
    background: var(--tally); border-radius: 2px; pointer-events: none;
  }
  .playhead::before {
    content: ""; position: absolute; top: -1px; left: -3px; width: 9px; height: 9px;
    background: var(--tally); border-radius: 50%;
  }

  .toolbar {
    display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 20px;
    padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  }
  .spacer { flex: 1; }
  .time {
    font-family: "JetBrains Mono", Consolas, monospace; font-weight: 500; font-size: 14px;
    font-variant-numeric: tabular-nums; background: var(--raise); color: var(--ink);
    padding: 9px 14px; border-radius: 10px;
  }
  .push { margin-left: auto; }

  details { margin-top: 24px; }
  summary { cursor: pointer; color: var(--muted); font-size: 14px; font-weight: 500; }
  #jsonView {
    background: var(--raise); border-radius: 12px; padding: 14px; margin: 12px 0 0;
    overflow: auto; max-height: 280px; font-size: 12px; line-height: 1.6;
    font-family: "JetBrains Mono", Consolas, monospace; color: var(--ink);
  }

  /* ---------- Trình xem code ---------- */
  .notes { color: var(--muted); font-size: 14px; white-space: pre-wrap; margin: 0 0 16px; }
  .projbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 0 0 12px; }
  .pj { display: inline-flex; align-items: stretch; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); overflow: hidden; max-width: 100%; }
  .pj.on { border-color: var(--accent); }
  .pj .pn { background: none; border: 0; border-radius: 0; padding: 6px 6px 6px 14px; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .pj.on .pn { color: var(--ink); }
  .pj .px { background: none; border: 0; border-radius: 0; padding: 6px 12px 6px 6px; font-size: 12px; color: var(--muted); cursor: pointer; }
  .pj .px:hover { color: var(--danger); }
  .editor { border-radius: 20px; overflow: hidden; background: var(--stage); border: 1px solid rgba(255, 255, 255, 0.06); }
  .ftabs { display: flex; overflow-x: auto; padding: 0 10px; border-bottom: 1px solid rgba(255, 255, 255, 0.08); }
  .ftab {
    background: transparent; color: #a3999a; border-radius: 0; padding: 13px 14px;
    border-bottom: 2px solid transparent; font-family: "JetBrains Mono", Consolas, monospace;
    font-size: 12.5px; font-weight: 500; white-space: nowrap;
  }
  .ftab:hover { color: #fff; }
  .ftab.active { color: #fff; border-bottom-color: var(--accent); }
  pre.code {
    margin: 0; padding: 22px 24px; background: var(--code-bg); color: var(--code-ink);
    font-family: "JetBrains Mono", "Cascadia Mono", Consolas, monospace;
    font-size: 13px; line-height: 1.7; max-height: 58vh; overflow: auto; white-space: pre; tab-size: 2;
  }

  /* ---------- Màn hình nhỏ ---------- */
  @media (max-width: 900px) {
    .top { grid-template-columns: 1fr auto; gap: 12px 8px; padding: 12px 16px; }
    .modes { grid-column: 1 / -1; grid-row: 2; display: flex; }
    .mode { flex: 1; }
    .icon-btn, .hdr-tools { grid-row: 1; grid-column: 2; }
    html { height: auto; overflow: visible; }
    body { height: auto; overflow: visible; }
    .shell { grid-template-columns: 1fr; height: auto; overflow: visible; }
    .side { position: static; height: auto; overflow: visible; border-right: 0; border-bottom: 1px solid var(--line); padding: 24px 16px 28px; }
    .work { height: auto; overflow: visible; padding: 20px 16px 40px; }
    .stage { padding: 26px 18px; min-height: 280px; }
    h1 { font-size: 24px; }
  }
  @media (prefers-reduced-motion: reduce) {
    /* Giảm chuyển động áp dụng cho phần trang trí; popup thông báo và vòng quay "đang tải" là tín hiệu cần thiết nên được giữ nguyên */
    *:not(.toast):not(.toast *):not(.loading):not(.modes):not(.modes *), *:not(.toast):not(.toast *):not(.loading):not(.modes):not(.modes *)::before { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
    .busy .progress::before { width: 100%; }
  }
  .shell.film { grid-template-columns: 1fr; } .shell.film .side { display: none; }
  .filmgrid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; align-items: start; }
  @media (max-width: 1100px) { .filmgrid { grid-template-columns: 1fr; } }
  .fpanel { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 18px; min-width: 0; }
  .fpanel h2 { margin: 0 0 6px; font-size: 17px; }
  .fpanel label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; font-weight: 600; }
  .fpanel textarea { resize: vertical; }
  .frow { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  .fshot { border-top: 1px solid var(--line); padding: 12px 0; margin-top: 12px; }
  .fshot img, .fshot video { width: 100%; display: block; border-radius: 12px; margin: 8px 0; background: var(--field); }
  .fshot button { width: 100%; }
  .fpanel .toolbar { margin-top: 14px; padding: 14px 0 0; background: none; border: 0; border-top: 1px solid var(--line); border-radius: 0; }
  .fbusy button { pointer-events: none; opacity: 0.5; }
  /* Nút tạm dừng: hiện khi đang tạo, bấm để dừng yêu cầu hiện tại */
  .genrow { display: flex; gap: 10px; align-items: stretch; }
  .genrow #gen { flex: 1 1 auto; min-width: 0; }
  .pause-btn { flex: 0 0 auto; background: transparent; color: var(--ink); border: 1px solid var(--line); border-radius: inherit; padding: 0 18px; white-space: nowrap; }
  .genrow .pause-btn { width: auto; padding: 0 20px; }   /* .big đặt width:100% làm nút Tạm dừng chiếm hết hàng và ép nút Gửi còn vài chục px */
  .genrow #gen { flex: 1 1 0; min-width: 110px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .pause-btn[hidden] { display: none; }
  .regen-btn { flex: 0 0 auto; background: transparent; color: var(--ink); border: 1px solid var(--line); border-radius: inherit; padding: 0 18px; white-space: nowrap; width: auto; }
  .regen-btn[hidden] { display: none; }
  /* Hàng nút chính: Gửi / Tạo lại / Tạm dừng cùng cỡ gọn, cùng kiểu */
  .genrow > button.big { width: auto; height: 40px; padding: 0 16px; font-size: 14px; font-weight: 600; line-height: 1; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
  .genrow > #gen.big { flex: 1 1 0; min-width: 96px; }
  .genrow > .regen-btn.big, .genrow > .pause-btn.big { flex: 0 0 auto; }
  .regen-btn:hover:not(:disabled) { border-color: var(--ink); background: var(--raise); }
  .macts { display: flex; gap: 6px; margin-top: 8px; opacity: .6; transition: opacity .15s ease; }
  .msg.user .macts { justify-content: flex-end; }
  .msg:hover .macts, .macts:focus-within { opacity: 1; }
  .macts button { background: transparent; color: var(--muted); border: 1px solid var(--line); border-radius: 8px; padding: 3px 10px; font-size: 12.5px; cursor: pointer; }
  .macts button:hover { color: var(--ink); border-color: var(--ink); background: var(--raise); }
  @media (hover: none) { .macts { opacity: 1; } }
  .pause-btn:hover:not(:disabled) { border-color: var(--ink); background: var(--raise); }
  /* Bật/tắt tab */
  .mode[hidden] { display: none !important; }
  .mode.cfg { padding: 8px 10px; display: inline-flex; align-items: center; }
  .mode.cfg svg { width: 18px; height: 18px; }
  .tab-cfg-panel { position: fixed; top: 76px; right: 16px; z-index: 60; background: var(--raise); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; box-shadow: 0 12px 32px rgba(0,0,0,.35); min-width: 200px; display: grid; gap: 10px; }
  .tab-cfg-panel[hidden] { display: none; }
  .tab-cfg-panel h4 { margin: 0; font-size: 13px; color: var(--muted); font-weight: 600; }
  .tab-cfg-panel label { display: flex; align-items: center; gap: 10px; cursor: pointer; color: var(--ink); }

  /* Tài khoản Google: cùng chiều cao với nút giao diện, nằm trên thanh đầu */
  .hdr-tools { display: flex; align-items: center; gap: 8px; }
  @media (max-width: 600px) { .top .acct { grid-column: 1 / -1; width: 100% !important; } }
  .acct { display: flex; align-items: center; min-width: 0; }
  .acct { align-self: stretch; }
  .acct .chip { width: 100%; height: 100%; box-sizing: border-box; }
  .acct .gwrap { width: auto; height: 100%; min-width: 150px; padding: 0 16px; box-sizing: border-box; }
  .acct .gwrap { justify-content: center; }
  .acct .chip { justify-content: space-between; }
  .acct .chip { height: 40px; display: inline-flex; align-items: center; gap: 8px; background: var(--raise); border: 1px solid var(--line); border-radius: 12px; padding: 0 4px 0 4px; }
  .acct .av { width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; font-weight: 700; font-size: 13px; flex: none; }
  .acct .who { color: var(--ink); font-size: 13px; font-weight: 500; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .acct .out { height: 32px; background: transparent; border: 0; color: var(--muted); border-radius: 9px; padding: 0 10px; cursor: pointer; font: inherit; font-size: 12.5px; }
  .acct .out:hover { color: var(--ink); background: var(--surface); }
  .acct .gwrap { position: relative; height: 40px; display: inline-flex; align-items: center; gap: 8px; padding: 0 14px; background: var(--raise); border: 1px solid var(--line); border-radius: 12px; color: var(--ink); font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap; }
  .acct .gwrap:hover { border-color: var(--accent); }
  .acct .gover { position: absolute; inset: 0; opacity: 0; overflow: hidden; border-radius: 12px; }
  .acct .gover iframe { width: 100% !important; height: 100% !important; }

  /* Màn hình đăng nhập */
  .login-ov { position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.6); backdrop-filter: blur(4px); display: grid; place-items: center; padding: 16px; }
  .login-ov[hidden] { display: none; }
  .login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 32px 28px 22px; text-align: center; box-shadow: 0 24px 60px rgba(0,0,0,.45); position: relative; overflow: hidden; }
  .login-card::before { content: ""; position: absolute; inset: 0 0 auto; height: 4px; background: var(--accent); }
  .login-card .lg-mark { width: 52px; height: 52px; margin: 0 auto 12px; border-radius: 15px; background: var(--raise); display: grid; place-items: center; color: var(--accent); }
  .login-card .lg-mark svg { width: 28px; height: 28px; }
  .login-card h2 { margin: 0 0 6px; font-size: 20px; font-weight: 800; color: var(--ink); }
  .login-card p { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
  .lg-btn { position: relative; height: 44px; width: 280px; margin: 0 auto; }
  .lg-fake { position: absolute; inset: 0; display: inline-flex; align-items: center; justify-content: center; gap: 10px; background: var(--raise); border: 1px solid var(--line); border-radius: 12px; color: var(--ink); font-size: 14px; font-weight: 600; pointer-events: none; }
  .lg-btn:hover .lg-fake { border-color: var(--accent); }
  .lg-btn .gover { position: absolute; inset: 0; opacity: 0; overflow: hidden; border-radius: 12px; }
  .lg-btn .gover iframe { width: 100% !important; height: 100% !important; }
  .lg-later { margin-top: 14px; background: transparent; border: 0; color: var(--muted); cursor: pointer; font: inherit; font-size: 13px; }
  .lg-later:hover { color: var(--ink); }
  /* Nhóm đăng nhập + thanh chế độ nằm giữa thanh đầu, nút đăng nhập rộng bằng thanh chế độ */
  .nav-group { display: inline-flex; align-items: stretch; gap: 12px; grid-column: 3; justify-self: end; }
  .nav-group .modes { flex: none; white-space: nowrap; align-items: center; }
  @media (max-width: 600px) { .nav-group { display: contents; } }

  /* Nhóm đăng nhập + thanh chế độ + sáng/tối: một hàng, không chồng lên nhau, không xuống dòng */
  .nav-group { display: flex; flex-wrap: nowrap; align-items: stretch; gap: 12px; grid-column: 3; justify-self: end; }
  .nav-group .modes { flex: none; flex-wrap: nowrap; white-space: nowrap; align-items: center; }
  .acct { position: relative; flex: 0 0 auto; align-self: stretch; display: flex; align-items: stretch; width: auto !important; }
  .acct .gwrap { position: relative; font-family: inherit; cursor: pointer; width: auto; height: auto; min-width: 150px; padding: 0 18px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-size: 13px; font-weight: 600; background: var(--raise); border: 1px solid var(--line); border-radius: 12px; color: var(--ink); box-sizing: border-box; }
  .acct .gwrap:hover { border-color: var(--accent); }
  .acct .gwrap .gover { position: absolute; inset: 0; opacity: 0; overflow: hidden; border-radius: 12px; }
  .acct .gwrap .gover iframe { width: 100% !important; height: 100% !important; }
  .acct .chip { height: 100%; width: auto; }
  @media (max-width: 600px) { .nav-group { display: contents; } }

  button.lg-fake { pointer-events: auto; cursor: pointer; }