/* ---------------------------------------------------------------------------
   Bảng màu.

   Ba trạng thái giao diện, và thứ tự khai báo ở đây quyết định cái nào thắng:

     1. :root                              — bảng màu SÁNG, luôn được định nghĩa đầy đủ
     2. @media (prefers-color-scheme: dark)
        :root:not([data-theme="light"])    — theo hệ thống, trừ khi người dùng ép sáng
     3. :root[data-theme="dark"]           — người dùng ép tối, thắng cả hai trên

   Không màu nào chỉ tồn tại bên trong media query: nếu thế thì lúc người dùng ép
   sáng trên máy đang để chế độ tối, biến đó sẽ không có giá trị nào cả.
   --------------------------------------------------------------------------- */
:root {
  color-scheme: light;
  --bg: #f6f6f8;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --border: #dededf;
  --text: #17171a;
  --muted: #66666f;
  --accent: #2f5fdd;
  --accent-fg: #ffffff;
  --danger: #b3261e;
  --ok: #14663a;
  --warn: #8a5a00;
  --warn-bg: #fff4dc;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 6px 20px rgba(0, 0, 0, .04);

  /* Vùng chạm tối thiểu trên iOS. Nhỏ hơn 44px là bấm trượt. */
  --tap: 44px;
  /* Lề an toàn: Dynamic Island ở trên, thanh home ở dưới, tai thỏ khi xoay ngang. */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121216;
    --surface: #1b1b21;
    --surface-2: #24242c;
    --border: #33333d;
    --text: #ecedf1;
    --muted: #9b9ba6;
    --accent: #8aa6ff;
    --accent-fg: #10121a;
    --danger: #ff9a90;
    --ok: #6ee7a8;
    --warn: #ffca6b;
    --warn-bg: #34290f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121216;
  --surface: #1b1b21;
  --surface-2: #24242c;
  --border: #33333d;
  --text: #ecedf1;
  --muted: #9b9ba6;
  --accent: #8aa6ff;
  --accent-fg: #10121a;
  --danger: #ff9a90;
  --ok: #6ee7a8;
  --warn: #ffca6b;
  --warn-bg: #34290f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

html {
  /* iOS Safari tự phóng chữ khi xoay ngang nếu không chặn. */
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  /* dvh thay vì vh: trên iOS Safari, 100vh tính cả phần bị thanh địa chỉ che, nên
     nội dung bị cắt mất một khúc dưới đáy. */
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto, sans-serif;
  /* Không bao giờ để cuộn ngang trên điện thoại. */
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent); }

/* --- Thanh điều hướng ---------------------------------------------------- */
header.site {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  /* Đệm thêm đúng bằng lề an toàn phía trên để không chui xuống dưới Dynamic Island. */
  padding: calc(10px + var(--safe-t)) calc(16px + var(--safe-r)) 10px calc(16px + var(--safe-l));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}
header.site .brand {
  font-weight: 650;
  text-decoration: none;
  color: var(--text);
  margin-right: auto;
  font-size: 17px;
}
header.site nav {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
header.site nav::-webkit-scrollbar { display: none; }
header.site nav a {
  display: flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 10px;
  border-radius: 9px;
  text-decoration: none;
  color: var(--muted);
  white-space: nowrap;
  font-size: 15px;
}
header.site nav a[aria-current="page"] {
  color: var(--text);
  background: var(--surface-2);
  font-weight: 600;
}
header.site .who {
  color: var(--muted);
  font-size: 13px;
  max-width: 14ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.theme-toggle {
  flex: 0 0 auto;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

/* Trên màn hẹp: giấu email và gom nav xuống một hàng riêng. */
@media (max-width: 560px) {
  header.site { flex-wrap: wrap; }
  header.site .who { display: none; }
  header.site nav {
    order: 3;
    width: 100%;
    padding-top: 2px;
  }
  header.site nav a { flex: 1 0 auto; justify-content: center; }
}

/* --- Bố cục -------------------------------------------------------------- */
main {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px calc(16px + var(--safe-r)) calc(48px + var(--safe-b)) calc(16px + var(--safe-l));
}
main.narrow {
  max-width: 440px;
  /* Trang đăng nhập không có header nên phải tự tránh Dynamic Island. */
  padding-top: calc(24px + var(--safe-t));
}

h1 { font-size: 24px; line-height: 1.25; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 18px; margin: 28px 0 10px; }
p.sub { color: var(--muted); margin: 0 0 20px; }

/* --- Form ---------------------------------------------------------------- */
label { display: block; font-size: 14px; font-weight: 550; margin: 16px 0 6px; }

input, select, textarea, button { font-family: inherit; color: inherit; }

input, select, textarea {
  width: 100%;
  /* 16px là ngưỡng: dưới mức này iOS Safari TỰ PHÓNG TO trang khi chạm vào ô nhập,
     và người dùng phải tự thu lại bằng tay. Đây là lý do duy nhất cỡ chữ này cố định. */
  font-size: 16px;
  min-height: var(--tap);
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 34px;
}
textarea { min-height: 130px; resize: vertical; line-height: 1.5; }
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

button {
  min-height: var(--tap);
  padding: 10px 18px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); }
button.link {
  min-height: 0;
  padding: 4px 0;
  border: 0;
  background: none;
  color: var(--accent);
  text-decoration: underline;
  font-size: 15px;
}
@media (hover: hover) {
  button:hover:not(:disabled) { border-color: var(--accent); }
}

/* Ô đánh dấu — hàng ngang, cả dòng chữ đều bấm được. */
.check {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0 0;
  min-height: var(--tap);
  font-size: 15px;
  font-weight: 400;
  cursor: pointer;
}
.check input[type="checkbox"] {
  width: 22px;
  height: 22px;
  min-height: 0;
  flex: 0 0 auto;
  accent-color: var(--accent);
  -webkit-appearance: auto;
  appearance: auto;
}
.check .hint { display: block; color: var(--muted); font-size: 13px; }

.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; min-width: 0; }
.row > .shrink { flex: 0 0 auto; }
.actions { display: flex; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.actions > a { display: contents; }

/* Trên điện thoại nút trải hết chiều ngang cho dễ bấm bằng ngón cái. */
@media (max-width: 560px) {
  .actions { gap: 8px; }
  .actions button { flex: 1 1 100%; }
}

/* --- Thẻ ----------------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.list { display: grid; gap: 12px; margin-top: 16px; }

.idea h3 { margin: 0 0 4px; font-size: 17px; line-height: 1.3; }
.idea h3 a { text-decoration: none; color: var(--text); }
.idea .hook { color: var(--muted); margin: 0 0 10px; }
.idea .meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }

.chip {
  font-size: 12px;
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-2);
  white-space: nowrap;
}
.chip.score { color: var(--ok); }
.chip.pending { color: var(--warn); background: var(--warn-bg); border-color: transparent; }

/* --- Thanh đồng bộ ------------------------------------------------------- */
/* Nổi bật hơn thẻ thường: đây là hành động người dùng phải chủ động làm, vì việc
   index cố ý không tự chạy khi lưu ý tưởng. */
.sync {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 16px;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
}
.sync.pending { background: var(--warn-bg); border-color: transparent; }
.sync .sync-text { flex: 1 1 200px; min-width: 0; font-size: 15px; }
.sync .sync-text strong { display: block; }
.sync .sync-text span { color: var(--muted); font-size: 13px; }
.sync.pending .sync-text strong { color: var(--warn); }
.sync button { flex: 0 0 auto; }
@media (max-width: 560px) { .sync button { flex: 1 1 100%; } }

/* --- Thông báo ----------------------------------------------------------- */
.msg { padding: 11px 14px; border-radius: 10px; margin: 14px 0; font-size: 15px; }
.msg.error { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.msg.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.msg.note { background: var(--warn-bg); color: var(--warn); }
.msg:empty { display: none; }

.empty { color: var(--muted); text-align: center; padding: 44px 20px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.spaced { margin-top: 18px; }

/* --- Bảng phiên ---------------------------------------------------------- */
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { text-align: left; padding: 12px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 550; font-size: 13px; }

/* Màn hẹp: bảng đổi thành danh sách xếp dọc. Bảng ba cột trên 402px chỉ có cách
   cuộn ngang hoặc bóp chữ tí xíu, cả hai đều tệ hơn. */
@media (max-width: 560px) {
  table thead { display: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  tr {
    border-bottom: 1px solid var(--border);
    padding: 12px 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 10px;
  }
  td { border: 0; padding: 0; }
  td:first-child { flex: 1 1 100%; font-weight: 550; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* Hàng chứa nút giao diện trên các trang không có thanh điều hướng. */
.topbar { display: flex; justify-content: flex-end; margin-bottom: 8px; }
