:root {
  --bg: #e8edf2;
  --panel: #ffffff;
  --ink: #1a2332;
  --muted: #5c6b7a;
  --line: #c5d0db;
  --header: #1f4e79;
  --accent: #0b6e4f;
  --accent-hover: #095c42;
  --danger: #b42318;
  --ok: #067647;
  --result: #0b2e59;
  --late: #fde2e7;
  --half: #e6ebf0;
  --font: "Segoe UI", "Be Vietnam Pro", Tahoma, sans-serif;
  --mono: "Cascadia Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; min-height: 100%;
  background: var(--bg); color: var(--ink);
  font-family: var(--font); font-size: 14px;
}

.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px; height: 48px;
  background: #12324f; color: #fff;
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 15px; }

.tabs { display: flex; gap: 4px; }
.tab {
  border: 0; background: transparent; color: #c9d7e6;
  padding: 8px 14px; border-radius: 6px; cursor: pointer; font: inherit;
}
.tab:hover { background: rgba(255,255,255,0.08); color: #fff; }
.tab.active { background: #fff; color: #12324f; font-weight: 600; }

main { padding: 16px 0 24px; }
.panel { display: none; padding: 0 16px; }
.panel.active { display: block; }

.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  height: 34px; padding: 0 14px; border-radius: 6px; cursor: pointer;
  font: inherit; font-weight: 600; white-space: nowrap;
}
.btn:hover { filter: brightness(0.97); }
.btn:disabled { opacity: 0.55; cursor: wait; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; color: inherit; }

input {
  height: 34px; border: 1px solid var(--line); border-radius: 6px;
  padding: 0 10px; font: inherit; color: var(--ink); background: #fff;
}
input[readonly] { background: #f1f4f7; }

.lbl { font-weight: 600; white-space: nowrap; }

/* Tab 1 */
.mcc-grid {
  display: grid;
  grid-template-columns: 150px 440px 190px;
  gap: 12px 14px; align-items: center;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 16px; width: max-content; max-width: 100%;
}
.mcc-grid .btn { width: 100%; }
.fld { display: flex; align-items: center; gap: 10px; min-width: 0; }
.fld.range .mid { font-weight: 600; white-space: nowrap; }

.ip-box { position: relative; display: inline-flex; align-items: center; }
.ip-box input { width: 210px; padding-right: 36px; font-family: var(--mono); }
.icon-btn {
  position: absolute; right: 4px; border: 0; background: transparent;
  cursor: pointer; font-size: 14px; padding: 4px 6px;
}

.status { font-weight: 700; }
.status.on { color: var(--ok); }
.status.off { color: var(--danger); }
.time-val { font-family: var(--mono); font-weight: 600; }

.result-bar {
  margin-top: 12px; background: var(--result); color: #fff;
  padding: 10px 14px; border-radius: 6px; min-height: 40px;
  font-weight: 600; white-space: pre-wrap; max-width: 100%;
}
.result-bar.err { color: #ffd2cc; }

/* Ô ngày giờ dd/mm/yyyy */
.dt { position: relative; display: inline-flex; align-items: center; }
.dt .dt-text { font-family: var(--mono); padding-right: 34px; }
.dt[data-kind="date"] .dt-text { width: 150px; }
.dt[data-kind="datetime"] .dt-text { width: 250px; }
.dt .dt-cal {
  position: absolute; right: 4px; border: 0; background: transparent;
  cursor: pointer; font-size: 14px; padding: 4px 6px;
}
/* Ô ngày gốc của trình duyệt nằm trong suốt đè lên nút lịch: bấm vào là mở lịch
   trên mọi máy (điện thoại không cần showPicker). */
.dt .dt-native {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 30px; height: 30px; min-width: 0; margin: 0; padding: 0; border: 0;
  opacity: 0; cursor: pointer; font-size: 16px; -webkit-appearance: none; appearance: none;
}

/* Tab 2 */
.ts-filters {
  display: grid;
  grid-template-columns: repeat(3, max-content) max-content;
  gap: 6px 14px; align-items: center;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 12px 16px; margin-bottom: 12px;
  width: max-content; max-width: 100%;
}
#ts-emp { width: 160px; }

.ts-wrap {
  overflow: auto; background: #fff; border: 1px solid #8fa3b8;
  max-height: calc(100vh - 190px); width: max-content; max-width: 100%;
}
table.ts { border-collapse: collapse; }
.ts th, .ts td {
  border: 1px solid #9eb1c4; padding: 6px 12px; white-space: nowrap;
}
.ts thead th {
  background: var(--header); color: #fff; font-weight: 600;
  position: sticky; top: 0; z-index: 2;
}
.ts td { text-align: center; font-family: var(--mono); }
.ts td.detail { text-align: left; min-width: 260px; }
.ts td.late { background: var(--late); }
.ts td.half { background: var(--half); }
.ts tr.sun td { color: var(--muted); }
.ts .empty { text-align: center; color: var(--muted); padding: 24px; font-family: var(--font); }

/* Tab 3 — cùng kiểu bảng phân quyền của Workstation */
.perm-panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px; width: fit-content; max-width: 100%; margin: 0 auto;
}
.perm-panel h3 { margin: 0 0 10px; font-size: 16px; }
.perm-wrap { overflow: auto; }
table.perm { border-collapse: collapse; background: #fff; border: 1px solid var(--line); }
.perm th, .perm td {
  border: 1px solid var(--line); padding: 4px 10px; font-size: 13px;
  white-space: nowrap; vertical-align: middle;
}
.perm th { background: #b8d4ea; font-weight: 700; text-align: center; }
.perm th.subh { font-size: 11px; font-weight: 600; color: #334155; }
.perm td.c { text-align: center; }
.perm input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }
.perm tbody tr:hover td { background: #f1f5f9; }
.perm tr.add td { background: #f8fafc; }
.perm tr.add input.code { width: 9em; height: 28px; font-size: 13px; }
.perm .hint { color: #94a3b8; }
.perm .del {
  height: 26px; padding: 0 10px; border: 1px solid #fca5a5; border-radius: 6px;
  background: #fee2e2; color: #b91c1c; font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
}
.perm .empty { text-align: center; color: var(--muted); padding: 18px; }
.perm td.pick { position: relative; }
.perm .suggest {
  position: absolute; left: 6px; top: calc(100% - 2px); z-index: 30; min-width: 320px;
  max-height: 260px; overflow: auto; background: #fff; border: 1px solid #94a3b8;
  border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15); padding: 4px;
}
.perm .suggest .opt { padding: 5px 8px; border-radius: 5px; cursor: pointer; font-size: 13px; }
.perm .suggest .opt span { color: var(--muted); margin-left: 8px; font-size: 12px; }
.perm .suggest .opt:hover, .perm .suggest .opt.on { background: #e0f2fe; }
.perm-panel .perm-wrap { overflow: visible; }
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.btn:disabled { cursor: not-allowed; }

.toast {
  position: fixed; right: 14px; bottom: 14px; z-index: 80;
  background: #0f172a; color: #fff; padding: 10px 14px; border-radius: 8px;
  display: none; font-weight: 600;
}
.toast.show { display: block; }
.toast.err { background: var(--danger); }

@media (max-width: 860px) {
  .ts-filters, .perm-panel .perm-wrap { overflow-x: auto; }
}

/* ---------- Điện thoại ---------- */
@media (max-width: 640px) {
  html, body { font-size: 15px; }
  input, .btn { font-size: 16px; height: 40px; } /* 16px để iOS không tự phóng to */

  .topbar { flex-wrap: wrap; height: auto; gap: 8px; padding: 8px 12px 0; }
  .topbar > span { display: none; }
  .topbar [data-sg-userhead] { margin-left: auto; }
  .topbar .sguh-hi { display: none; }
  .topbar .sguh-avt { width: 34px; height: 34px; font-size: 14px; }
  .topbar .sguh-menu { height: 34px; padding: 0 10px; }
  .tabs { order: 3; flex: 1 0 100%; gap: 0; }
  .tab { flex: 1; padding: 10px 4px; border-radius: 8px 8px 0 0; white-space: nowrap; }

  main { padding: 12px 0 20px; }
  .panel { padding: 0 12px; }

  /* Tab 1: nhãn trên, ô nhập dưới, nút dài hết hàng */
  .mcc-grid { grid-template-columns: 1fr; width: 100%; gap: 6px; padding: 14px 12px; }
  .mcc-grid > div:empty { display: none; }
  .mcc-grid .lbl { margin-top: 8px; }
  .mcc-grid .lbl:first-child { margin-top: 0; }
  .mcc-grid .btn { margin-top: 2px; }
  .ip-box, .ip-box input { width: 100%; }
  .fld { flex-wrap: wrap; gap: 8px; }
  .fld > .dt { flex: 1 1 100%; }
  .fld.range > .dt { flex: 1 1 0; min-width: 0; }
  .fld.range .mid { font-weight: 400; color: var(--muted); }
  .dt .dt-text { width: 100% !important; min-width: 0; }
  .time-val { font-size: 17px; }

  /* Tab 2: nhãn bên trái, ô bên phải, nút dài hết hàng */
  .ts-filters {
    grid-template-columns: max-content 1fr; width: 100%; gap: 8px 10px; padding: 12px;
  }
  .ts-filters > :nth-child(1) { grid-area: 1 / 1; }
  .ts-filters > :nth-child(2) { grid-area: 2 / 1; }
  .ts-filters > :nth-child(3) { grid-area: 3 / 1; }
  .ts-filters > :nth-child(4) { display: none; }
  .ts-filters > :nth-child(5) { grid-area: 1 / 2; }
  .ts-filters > :nth-child(6) { grid-area: 2 / 2; }
  .ts-filters > :nth-child(7) { grid-area: 3 / 2; }
  .ts-filters > :nth-child(8) { grid-area: 4 / 1 / 5 / 3; }
  .ts-filters .dt, #ts-emp, #btn-load-ts { width: 100%; }
  .ts-wrap { width: 100%; max-height: none; }
  .ts th, .ts td { padding: 6px 8px; font-size: 13px; }
  .ts td.detail { min-width: 0; }

  /* Tab 3 */
  .perm-panel { width: 100%; padding: 12px; }
  .perm th, .perm td { padding: 6px 8px; }
  .perm .suggest { min-width: 260px; }
  .toast { left: 12px; right: 12px; text-align: center; }
}
