:root {
  color-scheme: light;
  --bg: #f6f7f4;
  --sidebar: #232525;
  --sidebar-muted: #b9bdb6;
  --panel: #ffffff;
  --text: #202426;
  --muted: #747971;
  --line: #d9dcd6;
  --surface: #fbfaf6;
  --soft: #f1eee5;
  --soft-hover: #e7e2d6;
  --gold: #d8b55b;
  --champagne: #9a7428;
  --champagne-dark: #7c5c1f;
  --blue: #315f86;
  --amber: #b88a2e;
  --red: #a94442;
  --shadow: 0 14px 32px rgba(32, 36, 38, 0.08);
}

* {
  box-sizing: border-box;
}

/* Bảo đảm thuộc tính hidden luôn ẩn, kể cả khi phần tử có display khác (vd .field
   dùng display:grid). Nếu thiếu, ô "Ngày tạo"/"Tháng tạo" sẽ hiện cùng lúc. */
[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button,
input,
textarea {
  font: inherit;
  letter-spacing: 0;
}

.app-shell {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: var(--sidebar);
  color: #f8f6ef;
  padding: 22px 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  font-weight: 700;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 36px;
  border-radius: 8px;
  background: var(--gold);
  color: var(--sidebar);
  font-size: 13px;
}

.nav-tabs {
  display: grid;
  gap: 8px;
  margin-top: 32px;
}

.nav-tab {
  width: 100%;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--sidebar-muted);
  text-align: left;
  padding: 0 12px;
  cursor: pointer;
}

.nav-tab:hover,
.nav-tab.active {
  background: rgba(216, 181, 91, 0.14);
  color: #ffffff;
}

.workspace {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.2;
}

h2 {
  font-size: 15px;
  line-height: 1.2;
}

h3 {
  margin: 0;
  font-size: 14px;
  line-height: 1.2;
}

#viewMeta {
  color: var(--muted);
  margin-top: 4px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 132px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  padding: 0 12px;
  white-space: nowrap;
}

.status-pill.ok {
  border-color: rgba(154, 116, 40, 0.28);
  color: var(--champagne-dark);
}

.status-pill.warn {
  border-color: rgba(199, 132, 20, 0.28);
  color: var(--amber);
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.work-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.panel {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.field {
  display: grid;
  gap: 7px;
  min-width: 0;
  color: var(--muted);
  font-weight: 600;
}

.field span {
  overflow-wrap: anywhere;
  line-height: 1.35;
}

textarea,
input {
  min-width: 0;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

textarea {
  min-height: 292px;
  resize: vertical;
  padding: 12px;
  line-height: 1.5;
}

input {
  min-height: 42px;
  padding: 0 12px;
}

.field select {
  min-width: 0;
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0 30px 0 10px;
  outline: none;
  cursor: pointer;
}

textarea:focus,
input:focus,
.field select:focus {
  border-color: rgba(154, 116, 40, 0.62);
  box-shadow: 0 0 0 3px rgba(154, 116, 40, 0.14);
}

.supported-domains {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.subid-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
}

.links-filterbar {
  display: flex;
  align-items: end;
  gap: 10px;
  margin-bottom: 14px;
}

.date-filter {
  width: 190px;
}

.links-summary {
  display: flex;
  gap: 18px;
  margin-left: auto;
  text-align: right;
}

.summary-stat {
  display: grid;
  gap: 2px;
  min-width: 80px;
}

.links-summary span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.links-summary strong {
  color: var(--champagne-dark);
  font-size: 25px;
  line-height: 1;
}

.primary-button,
.secondary-button {
  min-height: 38px;
  border-radius: 8px;
  padding: 0 14px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 700;
}

.primary-button {
  background: var(--champagne);
  color: #ffffff;
}

.primary-button:hover {
  background: var(--champagne-dark);
}

.secondary-button {
  background: var(--soft);
  color: var(--text);
  border-color: var(--line);
}

.secondary-button:hover {
  border-color: #c8c0b2;
}

button:disabled {
  opacity: 0.62;
  cursor: wait;
}

.result-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.result-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface);
}

.result-item.warning {
  border-color: #d9a7a2;
  background: #fdf6f5;
}

.result-item.warning .result-label {
  color: #8f2d2d;
}

.result-row {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  gap: 10px;
  margin-top: 8px;
}

.result-row:first-child {
  margin-top: 0;
}

.result-label {
  color: var(--muted);
  font-weight: 700;
}

.mono {
  overflow-wrap: anywhere;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 12px;
}

.link-cell {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.link-cell a {
  min-width: 0;
  flex: 1;
}

.copy-link-button {
  min-width: 54px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}

.copy-link-button:hover {
  border-color: #c8c0b2;
}

/* Nhãn nhỏ cho biết link thuộc sàn nào (Shopee / Lazada). */
.net-badge {
  flex: none;
  align-self: center;
  margin-left: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid currentColor;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.link-cell .net-badge {
  margin-left: 0;
}

.net-badge.net-shopee {
  color: #d2691e;
}

.net-badge.net-lazada {
  color: #7b3fe4;
}

.mobile-final-url {
  display: none;
}

.table-wrap {
  overflow-x: auto;
}

.pagination-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 16px;
  color: var(--muted);
  font-size: 13px;
}

.pagination-controls,
.page-size-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-size-control select {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0 30px 0 10px;
}

.icon-button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  color: var(--text);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.icon-button:disabled {
  cursor: default;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 980px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 10px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

a {
  color: var(--blue);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.settings-panel {
  max-width: 920px;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.settings-section {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.settings-section .subid-grid {
  margin-top: 14px;
}

.empty-table {
  padding: 28px 12px;
  color: var(--muted);
  text-align: center;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(420px, calc(100vw - 40px));
  border-radius: 8px;
  background: var(--sidebar);
  color: #ffffff;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  transform: translateY(18px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast.warning {
  background: #8f2d2d;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px;
  }

  .nav-tabs {
    grid-template-columns: repeat(3, minmax(82px, 1fr));
    margin-top: 0;
  }

  .nav-tab {
    text-align: center;
  }

  .workspace {
    padding: 18px;
  }

  .work-grid,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .subid-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  /* iOS Safari tự phóng to khi chạm ô nhập có cỡ chữ < 16px. Đặt 16px để tránh
     bị zoom (ví dụ khi chạm ô mật khẩu lúc đăng nhập) rồi kẹt ở mức phóng to. */
  input,
  textarea,
  select {
    font-size: 16px;
  }

  .links-panel .table-wrap {
    overflow: visible;
  }

  .links-panel table,
  .links-panel tbody {
    display: block;
    min-width: 0;
    width: 100%;
  }

  .links-panel thead {
    display: none;
  }

  .links-panel tbody {
    display: grid;
    gap: 10px;
  }

  .links-panel tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
  }

  .links-panel td {
    display: grid;
    gap: 4px;
    min-width: 0;
    padding: 0;
    border: 0;
    overflow-wrap: anywhere;
  }

  .links-panel td::before {
    content: attr(data-label);
    color: var(--muted);
    font-family: Inter, ui-sans-serif, system-ui, sans-serif;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
  }

  .links-panel .short-cell,
  .links-panel .affiliate-cell,
  .links-panel .subids-cell,
  .links-panel .final-cell {
    grid-column: 1 / -1;
  }

  .links-panel .short-cell {
    padding-bottom: 10px;
    border-bottom: 1px solid var(--line);
  }

  .links-panel .empty-row {
    display: block;
  }

  .links-panel .empty-table::before {
    display: none;
  }

  .link-cell {
    align-items: center;
  }

  .copy-link-button {
    min-height: 40px;
  }

  .desktop-final-url {
    display: none;
  }

  .mobile-final-url {
    display: block;
  }

  .mobile-final-url summary {
    color: var(--blue);
    cursor: pointer;
    font-family: Inter, ui-sans-serif, system-ui, sans-serif;
    font-weight: 700;
  }

  .mobile-final-url div {
    margin-top: 8px;
    padding: 9px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #ffffff;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 560px) {
  .sidebar {
    align-items: stretch;
    flex-direction: column;
    position: sticky;
    top: 0;
    z-index: 10;
    padding: 10px 12px;
    box-shadow: 0 6px 18px rgba(32, 36, 38, 0.16);
  }

  .brand {
    min-height: 32px;
  }

  .brand-mark {
    width: 38px;
    height: 32px;
  }

  .nav-tabs {
    gap: 6px;
  }

  .nav-tab {
    min-height: 44px;
    padding: 0 8px;
  }

  .topbar {
    margin-bottom: 12px;
  }

  h1 {
    font-size: 20px;
  }

  .status-pill {
    min-width: 0;
    min-height: 32px;
    font-size: 12px;
  }

  .workspace {
    padding: 12px;
  }

  .work-grid {
    gap: 12px;
  }

  .panel {
    padding: 14px;
    box-shadow: 0 8px 22px rgba(32, 36, 38, 0.07);
  }

  .primary-button,
  .secondary-button,
  input {
    min-height: 44px;
  }

  textarea {
    height: 190px;
    min-height: 180px;
  }

  .actions > button {
    flex: 1;
  }

  .result-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .subid-grid {
    grid-template-columns: 1fr;
  }

  .pagination-bar {
    align-items: flex-start;
    flex-direction: column;
  }

  .pagination-controls {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) 44px;
    width: 100%;
  }

  .page-size-control {
    grid-column: 1 / -1;
    justify-content: space-between;
  }

  .page-size-control select,
  .icon-button {
    min-height: 44px;
  }

  #linksPageInfo {
    align-self: center;
    text-align: center;
  }

  /* Xếp các ô lọc thành lưới 2 cột cho gọn: hàng 1 Lọc theo | ngày/tháng,
     hàng 2 Sắp xếp | Refresh, hàng 3 phần tổng kết chiếm cả 2 cột. */
  .links-filterbar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: end;
    gap: 10px;
  }

  .date-filter {
    width: 100%;
  }

  .links-filterbar .secondary-button {
    width: 100%;
  }

  .links-summary {
    grid-column: 1 / -1;
    width: 100%;
    margin-left: 0;
    padding: 10px 0;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    text-align: left;
  }

  .toast {
    right: 12px;
    bottom: 12px;
    max-width: calc(100vw - 24px);
  }
}

/* Nhãn trạng thái + nút đăng xuất ở góc phải topbar */
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logout-button {
  display: grid;
  place-items: center;
  color: var(--muted);
}

.logout-button svg {
  display: block;
}

.logout-button:hover {
  border-color: #c8c0b2;
  color: var(--text);
}

/* Màn hình đăng nhập che toàn bộ giao diện khi chưa có phiên hợp lệ */
.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(32, 36, 38, 0.58);
  backdrop-filter: blur(2px);
}

.login-overlay[hidden] {
  display: none;
}

.login-card {
  width: 100%;
  max-width: 360px;
  display: grid;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 26px 24px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
}

.login-hint {
  color: var(--muted);
  margin-top: -4px;
}

.login-card .primary-button {
  margin-top: 4px;
}

.login-error {
  color: var(--red);
  font-weight: 600;
}

.login-error[hidden] {
  display: none;
}
