/*
 * KoreNet Tenant Portal — UX enhancement layer.
 * Self-hosted, zero external dependencies (CSP: script-src 'self' 'unsafe-inline').
 * Implements: dark/light theming, skeletons, toast queue, command palette,
 * universal search, notification center, contextual help drawer, favorites,
 * global progress bar, empty states, keyboard-shortcut cheat sheet, a11y
 * primitives (skip link, focus-visible, reduced motion) and print styles.
 *
 * All colors derive from the portal's existing token set (base.html :root).
 * Dark theme overrides those tokens under [data-theme="dark"].
 */

/* ---- Theming: dark mode overrides the base token set --------------------- */
:root {
  --ux-surface: var(--paper);
  --ux-elevated: #ffffff;
  --ux-border: rgba(10, 26, 58, 0.15);
  --ux-text: var(--ink);
  --ux-muted: #5b5b5b;
  --ux-accent: var(--gold);
  --ux-accent-ink: var(--navy);
  --ux-overlay: rgba(10, 26, 58, 0.55);
  --ux-shadow: 0 10px 40px rgba(10, 26, 58, 0.22);
  --ux-radius: 8px;
}

html[data-theme="dark"] {
  --navy: #0b1220;
  --navy-2: #131d33;
  --cream: #10161f;
  --paper: #161f2e;
  --ink: #e8ecf3;
  --ok: #3ecf7a;
  --danger: #e26868;
  --ux-surface: #161f2e;
  --ux-elevated: #1d283a;
  --ux-border: rgba(230, 190, 67, 0.18);
  --ux-text: #e8ecf3;
  --ux-muted: #9fb0c9;
  --ux-overlay: rgba(0, 0, 0, 0.66);
  --ux-shadow: 0 10px 44px rgba(0, 0, 0, 0.55);
}
html[data-theme="dark"] body {
  background: var(--cream);
}
html[data-theme="dark"] .card { background: var(--ux-surface); }
html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
  background: #0e1520;
  color: var(--ux-text);
  border-color: var(--ux-border);
}
html[data-theme="dark"] code,
html[data-theme="dark"] pre,
html[data-theme="dark"] .mono { background: rgba(255, 255, 255, 0.06); }

/* ---- Accessibility: skip link + focus visibility ------------------------- */
.ux-skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 10000;
  background: var(--navy);
  color: var(--gold);
  padding: 10px 16px;
  border: 2px solid var(--gold);
  border-radius: 0 0 6px 0;
  font-size: 13px;
  letter-spacing: 1px;
}
.ux-skip-link:focus { left: 0; }

/* Restore a strong, keyboard-only focus ring (base.html strips none). */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--gold-2);
  outline-offset: 2px;
  border-radius: 3px;
}

.ux-visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}

/* ---- Enhancement toolbar (injected into header) -------------------------- */
.ux-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: 14px;
}
.ux-icon-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  padding: 0;
  background: rgba(201, 162, 39, 0.12);
  border: 1px solid var(--gold);
  border-radius: 6px;
  color: var(--gold);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
}
.ux-icon-btn:hover { background: rgba(201, 162, 39, 0.24); color: var(--gold-2); }
.ux-icon-btn .ux-badge {
  position: absolute;
  top: -6px; right: -6px;
  min-width: 17px; height: 17px;
  padding: 0 4px;
  background: var(--danger);
  color: #fff;
  border-radius: 99px;
  font-size: 10px;
  font-weight: 700;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 17px;
}
.ux-icon-btn .ux-badge.show { display: inline-flex; }

.ux-search-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 190px;
  padding: 7px 12px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--gold);
  border-radius: 6px;
  color: var(--cream);
  cursor: text;
  font-size: 12px;
  letter-spacing: 0.5px;
}
.ux-search-trigger .ux-kbd {
  margin-left: auto;
  font-size: 10px;
  padding: 1px 6px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 4px;
  color: var(--cream);
  opacity: 0.8;
}
@media (max-width: 780px) {
  .ux-search-trigger { min-width: 0; }
  .ux-search-trigger .ux-search-label,
  .ux-search-trigger .ux-kbd { display: none; }
}

/* ---- Global progress bar ------------------------------------------------- */
.ux-progress {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 0;
  background: linear-gradient(90deg, var(--gold), var(--gold-2));
  z-index: 10001;
  opacity: 0;
  transition: width 0.2s ease, opacity 0.3s ease;
  box-shadow: 0 0 8px rgba(230, 190, 67, 0.7);
}
.ux-progress.active { opacity: 1; }

/* ---- Overlay + shared modal shell (palette, panels) ---------------------- */
.ux-overlay {
  position: fixed;
  inset: 0;
  background: var(--ux-overlay);
  z-index: 9998;
  display: none;
  backdrop-filter: blur(2px);
}
.ux-overlay.open { display: block; }

/* ---- Command palette / universal search ---------------------------------- */
.ux-palette {
  position: fixed;
  top: 12vh;
  left: 50%;
  transform: translateX(-50%);
  width: min(640px, 92vw);
  max-height: 70vh;
  background: var(--ux-elevated);
  color: var(--ux-text);
  border: 1px solid var(--ux-accent);
  border-radius: var(--ux-radius);
  box-shadow: var(--ux-shadow);
  z-index: 9999;
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.ux-palette.open { display: flex; }
.ux-palette input {
  border: none;
  border-bottom: 2px solid var(--ux-accent);
  border-radius: 0;
  padding: 16px 18px;
  font-size: 16px;
  width: 100%;
  background: transparent;
  color: var(--ux-text);
}
.ux-palette input:focus { outline: none; }
.ux-results { overflow-y: auto; padding: 6px 0; }
.ux-result-group-label {
  padding: 8px 18px 4px;
  font-size: 10px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--ux-muted);
  font-weight: 700;
}
.ux-result {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  cursor: pointer;
  font-size: 14px;
  border-left: 3px solid transparent;
}
.ux-result:hover,
.ux-result.active {
  background: rgba(201, 162, 39, 0.14);
  border-left-color: var(--ux-accent);
}
.ux-result .ux-result-icon { font-size: 15px; width: 20px; text-align: center; }
.ux-result .ux-result-body { flex: 1; min-width: 0; }
.ux-result .ux-result-title { font-weight: 600; }
.ux-result .ux-result-sub { font-size: 12px; color: var(--ux-muted); }
.ux-result mark { background: var(--gold-2); color: var(--navy); padding: 0 1px; border-radius: 2px; }
.ux-palette-empty { padding: 26px 18px; text-align: center; color: var(--ux-muted); font-size: 14px; }
.ux-palette-foot {
  border-top: 1px solid var(--ux-border);
  padding: 7px 16px;
  font-size: 11px;
  color: var(--ux-muted);
  display: flex;
  gap: 16px;
}

/* ---- Slide-in drawer (notifications, help) ------------------------------- */
.ux-drawer {
  position: fixed;
  top: 0; right: 0;
  height: 100vh;
  width: min(420px, 96vw);
  background: var(--ux-elevated);
  color: var(--ux-text);
  border-left: 1px solid var(--ux-accent);
  box-shadow: var(--ux-shadow);
  z-index: 9999;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
}
.ux-drawer.open { transform: translateX(0); }
.ux-drawer-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 2px solid var(--ux-accent);
}
.ux-drawer-head h3 { margin: 0; font-size: 16px; color: var(--navy); flex: 1; }
html[data-theme="dark"] .ux-drawer-head h3 { color: var(--gold); }
.ux-drawer-close {
  width: 30px; height: 30px; padding: 0;
  font-size: 16px; line-height: 1;
}
.ux-drawer-body { overflow-y: auto; padding: 8px 0; flex: 1; }
.ux-drawer-foot {
  border-top: 1px solid var(--ux-border);
  padding: 10px 16px;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.ux-notif {
  display: flex;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--ux-border);
  cursor: default;
}
.ux-notif.unread { background: rgba(201, 162, 39, 0.10); }
.ux-notif .ux-notif-dot {
  width: 8px; height: 8px; margin-top: 6px;
  border-radius: 99px; background: var(--ux-accent);
  flex-shrink: 0; visibility: hidden;
}
.ux-notif.unread .ux-notif-dot { visibility: visible; }
.ux-notif .ux-notif-body { flex: 1; min-width: 0; }
.ux-notif .ux-notif-title { font-weight: 600; font-size: 13.5px; }
.ux-notif .ux-notif-text { font-size: 12.5px; color: var(--ux-muted); margin-top: 2px; }
.ux-notif .ux-notif-time { font-size: 11px; color: var(--ux-muted); margin-top: 4px; }
.ux-notif .ux-sev {
  display: inline-block; font-size: 10px; letter-spacing: 1px;
  padding: 1px 6px; border-radius: 3px; text-transform: uppercase; font-weight: 700;
}
.ux-sev.critical { background: rgba(139,26,26,0.16); color: var(--danger); }
.ux-sev.high { background: rgba(201,120,20,0.16); color: #b5651d; }
.ux-sev.info { background: rgba(29,106,60,0.14); color: var(--ok); }

.ux-help-article { padding: 14px 18px; border-bottom: 1px solid var(--ux-border); }
.ux-help-article h4 { margin: 0 0 6px; color: var(--navy); font-size: 14px; }
html[data-theme="dark"] .ux-help-article h4 { color: var(--gold); }
.ux-help-article p { margin: 0 0 8px; font-size: 13px; line-height: 1.5; color: var(--ux-text); }
.ux-help-article ul { margin: 0 0 8px 18px; font-size: 13px; line-height: 1.5; }

/* ---- Favorites star ------------------------------------------------------ */
.ux-fav-star {
  background: transparent;
  border: none;
  color: var(--ux-muted);
  cursor: pointer;
  font-size: 18px;
  padding: 2px 6px;
  line-height: 1;
  text-transform: none;
  letter-spacing: 0;
}
.ux-fav-star:hover { color: var(--gold); background: transparent; }
.ux-fav-star.active { color: var(--gold); }

/* ---- Toast queue --------------------------------------------------------- */
.ux-toast-region {
  position: fixed;
  bottom: 20px; right: 20px;
  z-index: 10002;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: min(380px, 92vw);
}
.ux-toast {
  background: var(--navy);
  color: var(--cream);
  border: 1px solid var(--gold);
  border-left-width: 4px;
  border-radius: 6px;
  padding: 12px 14px;
  font-size: 13px;
  box-shadow: var(--ux-shadow);
  display: flex;
  gap: 10px;
  align-items: flex-start;
  animation: ux-toast-in 0.22s ease;
}
.ux-toast.success { border-left-color: var(--ok); }
.ux-toast.error { border-left-color: var(--danger); }
.ux-toast.info { border-left-color: var(--gold-2); }
.ux-toast .ux-toast-body { flex: 1; }
.ux-toast .ux-toast-action {
  background: transparent; border: none; color: var(--gold-2);
  padding: 0; font-size: 12px; text-decoration: underline; cursor: pointer;
  text-transform: none; letter-spacing: 0;
}
.ux-toast .ux-toast-x {
  background: transparent; border: none; color: var(--cream);
  cursor: pointer; font-size: 15px; padding: 0 2px; line-height: 1;
  text-transform: none; letter-spacing: 0;
}
@keyframes ux-toast-in {
  from { opacity: 0; transform: translateX(30px); }
  to { opacity: 1; transform: translateX(0); }
}
.ux-toast.leaving { opacity: 0; transform: translateX(30px); transition: all 0.2s ease; }

/* ---- Skeleton loading ---------------------------------------------------- */
.ux-skeleton {
  position: relative;
  overflow: hidden;
  background: rgba(10, 26, 58, 0.08);
  border-radius: 4px;
  min-height: 14px;
}
html[data-theme="dark"] .ux-skeleton { background: rgba(255, 255, 255, 0.08); }
.ux-skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.35), transparent);
  animation: ux-shimmer 1.3s infinite;
}
.ux-skeleton.line { height: 14px; margin: 8px 0; }
.ux-skeleton.line.short { width: 40%; }
.ux-skeleton.block { height: 120px; }
@keyframes ux-shimmer { 100% { transform: translateX(100%); } }

/* ---- Empty state --------------------------------------------------------- */
.ux-empty {
  text-align: center;
  padding: 44px 24px;
  color: var(--ux-muted);
}
.ux-empty .ux-empty-icon { font-size: 42px; opacity: 0.5; }
.ux-empty h3 { color: var(--navy); margin: 10px 0 6px; }
html[data-theme="dark"] .ux-empty h3 { color: var(--gold); }
.ux-empty p { max-width: 420px; margin: 0 auto 16px; font-size: 14px; line-height: 1.5; }

/* ---- Keyboard shortcut cheat sheet --------------------------------------- */
.ux-cheatsheet {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: min(520px, 92vw);
  background: var(--ux-elevated);
  color: var(--ux-text);
  border: 1px solid var(--ux-accent);
  border-radius: var(--ux-radius);
  box-shadow: var(--ux-shadow);
  z-index: 9999;
  display: none;
  padding: 22px 26px;
}
.ux-cheatsheet.open { display: block; }
.ux-cheatsheet h3 { margin: 0 0 14px; color: var(--navy); border-bottom: 2px solid var(--gold); padding-bottom: 6px; }
html[data-theme="dark"] .ux-cheatsheet h3 { color: var(--gold); }
.ux-cheatsheet dl { display: grid; grid-template-columns: auto 1fr; gap: 10px 18px; margin: 0; }
.ux-cheatsheet dt { text-align: right; }
.ux-cheatsheet dd { margin: 0; color: var(--ux-muted); }
.ux-kbd-key {
  display: inline-block;
  padding: 2px 8px;
  border: 1px solid var(--ux-border);
  border-bottom-width: 2px;
  border-radius: 4px;
  font-family: "JetBrains Mono", monospace;
  font-size: 12px;
  background: rgba(10, 26, 58, 0.05);
}
html[data-theme="dark"] .ux-kbd-key { background: rgba(255, 255, 255, 0.06); }

/* ---- Relative time ------------------------------------------------------- */
time[data-ux-time] { cursor: help; border-bottom: 1px dotted var(--ux-muted); }

/* ---- Reduced motion ------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  .ux-skeleton::after,
  .ux-toast,
  .ux-drawer,
  .ux-progress { animation: none !important; transition: none !important; }
}

/* ---- Print --------------------------------------------------------------- */
@media print {
  .classification-bar,
  header.top,
  footer,
  .ux-toolbar,
  .ux-toast-region,
  .ux-progress,
  .ux-overlay,
  .ux-palette,
  .ux-drawer,
  .ux-cheatsheet,
  .ux-skip-link,
  .ux-fav-star,
  a.button,
  button { display: none !important; }
  body { background: #fff !important; color: #000 !important; }
  main { max-width: 100% !important; padding: 0 !important; }
  .card { border: 1px solid #999 !important; box-shadow: none !important; break-inside: avoid; }
  a[href]::after { content: " (" attr(href) ")"; font-size: 10px; color: #555; }
}
