:root {
  color-scheme: dark;
  --bg: #07111f;
  --sidebar: #0b1728;
  --card: #101d30;
  --raised: #192a43;
  --line: #253854;
  --text: #f2f7ff;
  --muted: #9eb0c8;
  --subtle: #7891b0;
  --accent: #68a4ff;
  --accent-hover: #8ab9ff;
  --green: #55dda1;
  --red: #ff7f8f;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.5 Inter, ui-sans-serif, system-ui, sans-serif; }
body:not(.authenticated) #mainContent { max-width: 960px; margin: 0 auto; }
body:not(.authenticated) .site-header h1 { display: block; }
body.authenticated .site-header h1 { display: none; }
body.authenticated #mainContent { margin-left: 238px; }
#mainContent { min-width: 0; padding: 34px clamp(20px, 3vw, 48px) 76px; }
.site-header { margin-bottom: 28px; }
.eyebrow { color: var(--muted); font-size: 13px; }
h1 { margin: 20px 0 0; font-size: 30px; }
h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 29px; letter-spacing: -.025em; }
h3 { font-size: 17px; }
p { margin: 0; }
.muted, .note { color: var(--muted); }
.note { font-size: 12px; }
.bad { color: var(--red); }
.ok { color: var(--green); }
.hidden, .icon-library { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.icon { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.icon use[href="#icon-more"] { fill: currentColor; stroke: none; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 15px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.side-nav { position: fixed; inset: 0 auto 0 0; z-index: 20; width: 238px; padding: 21px 12px 16px; background: var(--sidebar); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; }
.brand-mark { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: var(--bg); font-size: 19px; font-weight: 800; }
.brand strong { display: block; font-size: 16px; }
.brand small { display: block; margin-top: 5px; color: var(--subtle); font-size: 10px; letter-spacing: .11em; }
.side-links { display: grid; gap: 7px; margin-top: 49px; }
.side-link { display: flex; align-items: center; gap: 13px; min-height: 42px; padding: 9px 12px; border-radius: 10px; color: var(--muted); text-decoration: none; font-size: 13px; }
.side-link:hover, .side-link.active { background: #1b3452; color: var(--text); }
.account-bar { display: flex; align-items: center; gap: 9px; margin-top: auto; padding: 15px 9px 0; border-top: 1px solid var(--line); min-width: 0; }
.avatar { display: grid; place-items: center; flex: none; width: 34px; height: 34px; border-radius: 50%; background: #284d77; color: var(--text); font-size: 11px; font-weight: 700; }
.account-details { min-width: 0; }
.account-details strong { display: block; max-width: 155px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.text-button { display: block; border: 0; padding: 0; background: transparent; color: var(--muted); font-size: 11px; font-weight: 400; text-align: left; }
.text-button:hover { color: var(--text); background: transparent; }
.mobile-topbar, .mobile-menu-button, .sidebar-backdrop { display: none !important; }

button { appearance: none; border: 1px solid transparent; border-radius: 9px; padding: 10px 14px; background: var(--accent); color: var(--bg); font: inherit; font-weight: 650; cursor: pointer; transition: background-color .15s ease, border-color .15s ease; }
button:hover { background: var(--accent-hover); }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .55; cursor: not-allowed; }
button.secondary, button.icon-button { background: var(--raised); border-color: var(--line); color: var(--text); }
button.secondary:hover, button.icon-button:hover { background: #214062; }
button.danger { background: #a43c50; color: #fff; }
button.danger:hover { background: #bd4c61; }
.icon-button { position: relative; display: inline-grid; place-items: center; width: 35px; height: 35px; padding: 0; flex: none; }
.icon-button[data-tooltip]:hover::after, .icon-button[data-tooltip]:focus-visible::after { content: attr(data-tooltip); position: absolute; left: 50%; bottom: calc(100% + 8px); transform: translateX(-50%); z-index: 30; width: max-content; max-width: 190px; padding: 6px 9px; border-radius: 7px; background: #234263; color: var(--text); font-size: 11px; font-weight: 400; white-space: nowrap; pointer-events: none; box-shadow: 0 8px 20px #0008; }
input, select { min-width: 0; border: 1px solid #355477; background: #111f33; color: var(--text); border-radius: 9px; padding: 10px 12px; font: inherit; }
label { display: grid; gap: 6px; margin: 14px 0; }
.auth { max-width: 440px; margin: 64px auto; padding: 24px; }
.auth h2 { font-size: 23px; }
.auth form > button { margin-top: 8px; }
.auth input { width: 100%; }

.section-title { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin: 30px 0 16px; }
.admin-primary-title { margin-top: 0; }
.section-description { margin-top: 6px; color: var(--muted); }
.section-actions { display: flex; flex-wrap: wrap; gap: 9px; }
.summary-grid { display: grid; grid-template-columns: 1fr 1fr 1.13fr; gap: 14px; margin-bottom: 26px; }
.summary-card { min-height: 106px; padding: 18px 21px; }
.summary-card span { display: block; font-size: 13px; }
.summary-card strong { display: block; margin-top: 15px; font-size: 27px; line-height: 1.1; }
#cycleReset { font-size: 18px; }
#keyReveal { margin: 0 0 19px; padding: 18px; }
#keyReveal code { display: block; margin-top: 12px; padding: 12px; overflow-wrap: anywhere; background: #08101e; border: 1px solid var(--line); border-radius: 9px; }
#keyReveal .note { margin-top: 8px; }
.account-list { padding: 0; }
.account-list-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 20px; }
.account-list-toolbar p { margin-top: 5px; font-size: 12px; }
.account-filters { display: flex; align-items: center; gap: 9px; }
.account-filters label { margin: 0; }
.search-field { display: flex; align-items: center; gap: 8px; width: 230px; padding: 0 10px; background: var(--raised); border: 1px solid #355477; border-radius: 9px; color: var(--muted); }
.search-field .icon { width: 17px; height: 17px; }
.search-field input { width: 100%; padding: 9px 0; border: 0; background: transparent; outline: none; }
.page-size-label select { width: 109px; }
.mobile-sort { display: none !important; }
.table-wrap { overflow: visible; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.col-alias { width: 23%; }.col-role { width: 11%; }.col-usage { width: 29%; }.col-status { width: 17%; }.col-actions { width: 20%; }
th, td { padding: 15px clamp(9px, 1.2vw, 18px); text-align: left; border-top: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-size: 11px; font-weight: 650; white-space: nowrap; text-transform: uppercase; letter-spacing: .02em; }
td { font-size: 13px; }
tr:hover td { background: #162943; }
.sort-button { display: inline-flex; align-items: center; gap: 5px; padding: 0; border: 0; background: transparent; color: var(--muted); font-size: 11px; text-transform: uppercase; white-space: nowrap; }
.sort-button:hover, .sort-button.active { background: transparent; color: var(--text); }
.sort-divider { margin: 0 5px; color: var(--subtle); }
.sort-indicator { font-size: 11px; }
.alias-cell .alias-wrap { display: flex; align-items: center; gap: 10px; min-width: 0; }
.alias-cell .avatar { width: 32px; height: 32px; }
.alias-details { flex: 1; min-width: 0; }
.alias-heading { display: flex; align-items: center; gap: 6px; min-width: 0; }
.alias-name { flex-shrink: 0; max-width: 65%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 650; }
.alias-hint { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; }
.role-badge { display: inline-block; padding: 4px 12px; border-radius: 20px; background: #1b3654; font-size: 11px; text-transform: capitalize; }
.usage-heading { display: flex; align-items: baseline; flex-wrap: wrap; gap: 2px 8px; }
.usage-number { font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.usage-bar { width: min(100%, 240px); height: 5px; margin-top: 9px; border-radius: 5px; background: #294361; overflow: hidden; }
.usage-bar > span { display: block; height: 100%; background: var(--accent); transition: width .2s ease; }
.usage-percent { color: var(--muted); font-size: 10px; white-space: nowrap; }
.model-label { flex: 0 1 auto; min-width: 0; max-width: 120px; padding: 2px 6px; border: 1px solid var(--line); border-radius: 6px; color: var(--accent); font-size: 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-unknown { color: var(--muted); }
.usage-note { display: block; margin-top: 5px; color: var(--muted); font-size: 11px; }
.status-label { display: inline-flex; align-items: center; gap: 8px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.status-dot.disabled { background: #fbbf24; }
.status-note { display: block; margin: 3px 0 0 15px; color: var(--muted); font-size: 11px; }
.actions { position: relative; }
.action-group { display: flex; align-items: center; gap: 7px; }
.actions .icon-button { width: 35px; height: 35px; }
.account-menu { position: absolute; top: calc(50% + 18px); right: 12px; z-index: 15; width: 205px; padding: 6px; border: 1px solid #355477; border-radius: 10px; background: #172b45; box-shadow: 0 16px 36px #0009; }
.account-menu button { display: block; width: 100%; padding: 9px 10px; border: 0; border-radius: 7px; background: transparent; color: var(--text); font-size: 12px; font-weight: 500; text-align: left; }
.account-menu button:hover, .account-menu button:focus-visible { background: #234263; }
.account-menu button.menu-danger { color: #ff9aa6; }
.account-menu hr { border: 0; border-top: 1px solid #355477; margin: 5px 0; }
.empty-row { padding: 32px; color: var(--muted); text-align: center; }
.pagination { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 20px; border-top: 1px solid var(--line); }
.pagination-controls { display: flex; gap: 8px; }
.pagination-controls button { width: 34px; height: 34px; padding: 0; font-size: 23px; line-height: 1; }
.page-info { font-size: 12px; }

.lower-section { margin-top: 45px; scroll-margin-top: 20px; }
body.authenticated:not(.admin-view) #personalQuota { margin-top: 0; }
#openaiQuota { padding: 19px; }
.price-note { margin: 12px 0 0; font-size: 12px; }
.hero { display: grid; grid-template-columns: minmax(200px, 1fr) 2fr; gap: 26px; padding: 22px; }
.big { margin: 7px 0 12px; color: var(--green); font-size: clamp(35px, 4vw, 55px); font-weight: 750; line-height: 1.1; overflow-wrap: anywhere; }
progress { width: 100%; height: 8px; border: 0; border-radius: 20px; overflow: hidden; background: #243651; }
progress::-webkit-progress-bar { background: #243651; }
progress::-webkit-progress-value { background: linear-gradient(90deg, var(--accent), var(--green)); }
progress::-moz-progress-bar { background: linear-gradient(90deg, var(--accent), var(--green)); }
.metrics { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
.metric { padding: 12px; border-radius: 10px; background: #192a43; }
.metric b { display: block; margin-top: 5px; font-size: 19px; overflow-wrap: anywhere; }
#openaiResult { grid-template-columns: repeat(3, minmax(0,1fr)); margin-bottom: 12px; }

dialog { width: min(92vw, 480px); max-height: min(90vh, 760px); padding: 0; border: 1px solid #355477; border-radius: 15px; background: var(--card); color: var(--text); box-shadow: 0 24px 80px #0009; }
dialog::backdrop { background: #020811c9; backdrop-filter: blur(3px); }
.dialog-card { padding: 23px; }
.dialog-card h2 { font-size: 21px; }
.dialog-card > p { margin-top: 10px; color: var(--muted); white-space: pre-wrap; }
.dialog-header { display: flex; align-items: center; justify-content: space-between; }
.dialog-card label { margin: 17px 0; }
.dialog-card input, .dialog-card select { width: 100%; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 24px; }

@media (max-width: 1100px) {
  .side-nav { width: 205px; }
  body.authenticated #mainContent { margin-left: 205px; }
  .col-alias { width: 22%; }.col-role { width: 10%; }.col-usage { width: 31%; }.col-status { width: 18%; }.col-actions { width: 19%; }
  th, td { padding-left: 10px; padding-right: 10px; }
}
@media (max-width: 850px) {
  body.authenticated #mainContent { margin-left: 0; padding: 94px 20px 56px; }
  .mobile-topbar:not(.hidden) { position: fixed; inset: 0 0 auto; z-index: 18; display: flex !important; align-items: center; justify-content: space-between; height: 66px; padding: 0 20px 0 64px; background: var(--sidebar); border-bottom: 1px solid var(--line); font-size: 17px; }
  .mobile-menu-button:not(.hidden) { position: fixed; top: 15px; left: 16px; z-index: 31; display: grid !important; }
  .side-nav:not(.hidden) { width: min(290px, 84vw); transform: translateX(-110%); transition: transform .2s ease; box-shadow: 16px 0 40px #0008; }
  body.sidebar-open .side-nav { transform: translateX(0); }
  body.sidebar-open .sidebar-backdrop:not(.hidden) { position: fixed; inset: 0; z-index: 19; display: block !important; background: #0009; }
  body.sidebar-open .mobile-menu-button { left: min(240px, calc(84vw - 50px)); }
  .site-header { display: none; }
  .admin-primary-title { margin-top: 0; }
  .section-title { align-items: stretch; flex-direction: column; gap: 15px; }
  .section-title > button { align-self: stretch; }
  .summary-grid { grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
  .summary-card { min-height: 82px; padding: 14px 16px; }
  .summary-card strong { margin-top: 9px; font-size: 24px; }
  .summary-card:last-child { grid-column: 1 / -1; }
  .account-list-toolbar { flex-direction: column; align-items: stretch; }
  .account-filters { display: grid; grid-template-columns: minmax(0,1fr) auto auto; }
  .search-field { width: 100%; grid-column: 1 / -1; }
  .mobile-sort { display: block !important; }
  .mobile-sort select { width: 100%; }
  button.mobile-sort { width: 41px; padding: 0; }
  .page-size-label select { width: 100px; }
  table, tbody { display: block; width: 100%; }
  colgroup, thead { display: none; }
  tbody { padding: 0 12px 12px; }
  tbody tr { position: relative; display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 0; margin-top: 12px; padding: 14px 15px; border: 1px solid var(--line); border-radius: 13px; background: #162943; }
  tbody td { padding: 0; border: 0; }
  tr:hover td { background: transparent; }
  .alias-cell { grid-column: 1; grid-row: 1; }
  .role-cell { grid-column: 1; grid-row: 2; margin: 3px 0 0 42px; }
  .role-badge { padding: 0; background: transparent; color: var(--muted); }
  .alias-hint { display: none; }
  .status-cell { grid-column: 2; grid-row: 1; align-self: start; margin: 4px 0 0 7px; font-size: 11px; }
  .status-note { max-width: 90px; }
  .usage-cell { grid-column: 1 / -1; grid-row: 3; margin-top: 14px; padding-top: 13px; border-top: 1px solid var(--line); }
  .usage-cell::before { content: "Đã dùng / Hạn mức"; display: block; margin-bottom: 3px; color: var(--muted); font-size: 11px; }
  .usage-bar { width: calc(100% - 126px); }
  .actions { grid-column: 2; grid-row: 3; align-self: center; margin-top: 15px; }
  .account-menu { top: calc(100% + 5px); right: 0; }
  .hero { grid-template-columns: 1fr; }
  #openaiResult { grid-template-columns: 1fr; }
  .lower-section { scroll-margin-top: 90px; }
}
@media (max-width: 520px) {
  .usage-bar { width: 100%; }
  .actions { grid-column: 1 / -1; grid-row: 4; justify-self: end; margin-top: 10px; }
  #mainContent { padding-left: 18px; padding-right: 18px; }
  .account-list-toolbar { padding: 16px; }
  .section-actions { flex-direction: column; }
  .section-actions button { width: 100%; }
  .metrics { grid-template-columns: 1fr 1fr; }
  .metric b { font-size: 15px; }
  .pagination { padding: 13px 16px; }
  .pagination-controls { gap: 5px; }
}
@media (max-width: 390px) {
  tbody tr { grid-template-columns: minmax(0,1fr) auto; }
  .alias-cell { grid-column: 1 / -1; grid-row: 1; }
  .role-cell { grid-column: 1; grid-row: 2; }
  .status-cell { grid-column: 2; grid-row: 2; }
  .usage-cell { grid-row: 3; }
  .usage-bar { width: 100%; }
  .actions { grid-column: 1 / -1; grid-row: 4; justify-self: end; margin-top: 10px; }
}

/* Keep the full guide readable without making the dashboard unbounded. */
.guide-download { display: inline-block; align-self: center; padding: 9px 13px; border: 1px solid var(--line); border-radius: 9px; color: var(--accent); text-decoration: none; white-space: nowrap; }
.guide-download:hover { background: var(--raised); }
.guide-content { height: min(65vh, 600px); min-height: 260px; overflow-y: auto; overscroll-behavior-y: contain; scrollbar-gutter: stable; padding: clamp(16px, 3vw, 28px); overflow-wrap: anywhere; }
.guide-content:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.guide-content h1 { margin: 0 0 18px; font-size: 24px; }
.guide-content h2 { margin: 28px 0 12px; font-size: 21px; }
.guide-content h3 { margin: 20px 0 10px; font-size: 17px; }
.guide-content p, .guide-content ul, .guide-content ol, .guide-content blockquote, .guide-content pre { margin: 12px 0; }
.guide-content ul, .guide-content ol { padding-left: 24px; }
.guide-content li { margin: 5px 0; }
.guide-content a { color: var(--accent); text-decoration: underline; }
.guide-content blockquote { padding: 4px 16px; border-left: 3px solid var(--accent); background: var(--raised); color: var(--muted); }
.guide-content code { padding: 2px 5px; border-radius: 4px; background: #08101e; font-size: .9em; }
.guide-content pre { padding: 15px; border: 1px solid var(--line); border-radius: 9px; overflow-x: auto; background: #08101e; }
.guide-content pre code { padding: 0; white-space: pre; background: transparent; }

#personalKeyValue { display: block; overflow-wrap: anywhere; user-select: all; margin-bottom: 16px; }
#personalKey { padding: 22px; margin-top: 16px; }
#personalKey .row { flex-wrap: wrap; }
