:root {
  --ink: #10213a;
  --muted: #66758b;
  --blue: #2f66e9;
  --blue-dark: #214fc1;
  --blue-soft: #eaf0ff;
  --navy: #14243d;
  --navy-raised: #203553;
  --line: #dfe7f2;
  --wash: #f4f7fc;
  --paper: #fff;
  --green: #138567;
  --amber: #d88a0c;
  --red: #ce4054;
  --app-bar-height: 56px;
  --tg-safe-area-top: 0px;
  --tg-safe-area-right: 0px;
  --tg-safe-area-bottom: 0px;
  --tg-safe-area-left: 0px;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--wash); }
body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--wash);
  color: var(--ink);
  font: 15px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
body.drawer-open { overscroll-behavior: none; }
a { color: #174fc5; text-underline-offset: 2px; }
a:hover { color: #103b96; }
main {
  width: auto;
  max-width: 1390px;
  min-width: 0;
  margin-left: 268px;
  padding: 42px 48px 64px;
}
h1 { margin: 0 0 24px; font-size: clamp(27px, 2.1vw, 34px); line-height: 1.13; letter-spacing: -.9px; }
h2 { margin: 30px 0 14px; font-size: 19px; line-height: 1.25; letter-spacing: -.25px; }
h3 { margin: 24px 0 12px; }
p { color: var(--muted); }
pre { max-width: 100%; overflow: auto; }
summary { cursor: pointer; font-weight: 700; color: var(--ink); }
details { margin: 14px 0; }
details > summary { min-height: 44px; padding: 11px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--paper); }
details[open] > summary { margin-bottom: 10px; border-color: #cbd8ee; }
.eyebrow { margin: 0 0 6px; color: var(--blue); font-size: 12px; text-transform: uppercase; letter-spacing: 1.2px; font-weight: 800; }

.app-bar { display: none; }
#main-nav {
  position: fixed;
  z-index: 40;
  inset: 16px auto 16px 16px;
  width: 228px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 18px 12px 12px;
  border-radius: 22px;
  background: linear-gradient(165deg, #172844 0%, var(--navy) 70%);
  color: #eaf0ff;
  box-shadow: 0 16px 42px rgb(18 34 58 / 13%);
}
.brand, .app-brand {
  display: flex;
  gap: 10px;
  align-items: center;
  color: #fff;
  text-decoration: none;
  font-size: 20px;
  letter-spacing: -.45px;
}
.brand { padding: 7px 10px 25px; }
.brand img, .app-brand img, .login-brand img { width: 30px; height: 30px; border-radius: 9px; object-fit: cover; }
.menu { display: grid; gap: 4px; min-height: 0; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
.menu a, .logout {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  padding: 10px;
  border-radius: 11px;
  color: #c7d3e7;
  text-decoration: none;
  font-weight: 650;
}
.menu a:hover, .menu a:focus-visible, .menu a[aria-current="page"] { background: #29405f; color: #fff; outline: none; }
.menu a[aria-current="page"] { box-shadow: inset 3px 0 #73a2ff; }
.menu i { flex: 0 0 18px; width: 18px; color: #8fb1ff; font-style: normal; text-align: center; }
.account { margin-top: auto; padding: 14px 10px 2px; border-top: 1px solid rgb(255 255 255 / 11%); display: grid; gap: 3px; }
.account small { color: #91a3bf; text-transform: uppercase; font-size: 10px; letter-spacing: 1px; }
.account strong { text-transform: capitalize; font-size: 14px; }
.logout { margin-top: 7px; padding-left: 0; color: #adc1df; }
.nav-close { display: none; }
.nav-backdrop[hidden] { display: none; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr)); gap: 14px; }
.card, .panel {
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--paper);
  box-shadow: 0 2px 5px rgb(18 36 61 / 4%);
}
.card { padding: 18px; border-radius: 16px; }
.panel { padding: 22px; border-radius: 18px; }
.metric small { color: var(--muted); font-weight: 650; }
.metric b { display: block; margin-top: 7px; font-size: clamp(23px, 2.2vw, 28px); line-height: 1.2; letter-spacing: -.7px; overflow-wrap: anywhere; }
.metric p, .metric-details { overflow-wrap: anywhere; }
.card-main { display: block; color: inherit; text-decoration: none; }
.card-main:focus-visible { outline: 3px solid rgb(47 102 233 / 23%); outline-offset: 3px; }
.card-main span { display: block; margin-top: 4px; color: var(--muted); font-size: 13px; }
.overview-cards .metric-details { margin-top: 10px; font-size: 12px; }
.operator-card:hover { box-shadow: 0 6px 20px rgb(18 36 58 / 10%); }
.empty { padding: 32px 18px; text-align: center; border: 1px dashed #c4d2e5; border-radius: 16px; background: #fbfcff; color: var(--muted); }
.notice { margin: 0 0 18px; padding: 12px 14px; border-radius: 11px; background: #fff6df; color: #8b5b05; overflow-wrap: anywhere; }

input, textarea, select, button {
  width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid #d4deec;
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}
input, select, button { min-height: 42px; }
input:focus, textarea:focus, select:focus, button:focus-visible, summary:focus-visible, a:focus-visible {
  outline: 3px solid rgb(47 102 233 / 22%);
  outline-offset: 1px;
  border-color: var(--blue);
}
textarea { min-height: 88px; resize: vertical; }
button { border-color: transparent; background: var(--blue); color: #fff; font-weight: 750; cursor: pointer; transition: background-color .15s, transform .15s; }
button:hover { background: var(--blue-dark); }
button:disabled { cursor: wait; opacity: .66; }
form { display: grid; gap: 10px; }
label { min-width: 0; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.form-grid > label { display: grid; align-content: start; gap: 4px; color: #526178; font-size: 13px; font-weight: 650; }
.form-grid .wide { grid-column: 1 / -1; }

.table-scroll, .users-scroll, .profile-orders, .numbers-panel { max-width: 100%; overflow-x: auto; overscroll-behavior-inline: contain; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: separate; border-spacing: 0; border: 1px solid var(--line); border-radius: 14px; background: #fff; }
th { background: #f7f9fd; color: #66748a; font-size: 11px; letter-spacing: .65px; text-transform: uppercase; }
td, th { padding: 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; }
tr:last-child td { border-bottom: 0; }
.order-list th:last-child, .order-list td:last-child { min-width: 125px; }
.order-list td:last-child .cell-value { white-space: nowrap; }
.status { display: inline-flex; padding: 3px 8px; border-radius: 999px; background: var(--blue-soft); color: #3159bf; font-size: 12px; font-weight: 700; }
.financial-table { min-width: 720px; }

.page-filter {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 12px;
  margin: 0 0 18px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--paper);
  box-shadow: 0 2px 5px rgb(18 36 58 / 4%);
}
.filter-presets { display: flex; align-items: end; gap: 7px; flex-wrap: wrap; }
.page-filter button, .page-filter input { width: auto; margin: 0; min-height: 38px; }
.filter-preset { padding: 7px 11px; background: #fff; color: var(--ink); border: 1px solid var(--line); white-space: nowrap; }
.filter-preset:hover { background: var(--blue-soft); color: var(--blue-dark); }
.filter-preset.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.filter-count { font-variant-numeric: tabular-nums; }
.filter-custom { margin: 0; }
.filter-custom-fields { display: flex; justify-content: flex-end; align-items: end; gap: 7px; }
.filter-custom label { display: grid; gap: 3px; color: var(--muted); font-size: 11px; font-weight: 700; }
.filter-custom summary { display: none; }
.advanced-filters { margin: 0 0 18px; padding: 14px; border: 1px solid var(--line); border-radius: 16px; background: var(--paper); }
.advanced-filters > summary { width: fit-content; margin: -6px; border: 0; background: transparent; color: var(--blue-dark); }
.advanced-filters[open] > summary { margin-bottom: 8px; }
.advanced-filters .form-grid { grid-template-columns: repeat(4, minmax(130px, 1fr)) auto; align-items: end; }
.advanced-filters .wide { grid-column: auto; }

.fx-equivalent { display: block !important; margin-top: 4px; color: var(--muted); font-size: 12px !important; font-weight: 400 !important; letter-spacing: normal !important; line-height: 1.5; }
.fx-source-note { margin-top: 24px; font-size: 12px; }
.button-link { display: inline-flex; align-items: center; justify-content: center; min-height: 42px; padding: 10px 14px; border-radius: 10px; background: var(--blue); color: #fff; font-weight: 750; text-decoration: none; }
.button-link:hover { background: var(--blue-dark); color: #fff; }
dialog { width: min(560px, calc(100% - 32px)); max-height: min(80dvh, 720px); overflow: auto; border: 1px solid var(--line); border-radius: 18px; color: var(--ink); box-shadow: 0 22px 60px rgb(8 24 48 / 28%); }
dialog::backdrop { background: rgb(8 24 48 / 52%); backdrop-filter: blur(2px); }
.modal-close { width: 44px; min-width: 44px; color: var(--ink); }

.users-scroll { border: 1px solid var(--line); border-radius: 14px; background: #fff; }
.users-table { min-width: 1320px; border: 0; border-radius: 0; }
.users-table th, .users-table td { vertical-align: top; padding: 10px; }
.users-table input, .users-table select { min-width: 105px; padding: 8px; }
.users-table [name="email"] { min-width: 190px; }
.users-table [name="display_name"] { min-width: 155px; }
.users-table [name="password"] { min-width: 150px; }
.user-roles { display: grid; gap: 4px; }
.user-roles label, .access-label { display: flex; gap: 6px; align-items: center; white-space: nowrap; font-size: 12px; }
.user-roles input, .access-label input { width: auto; min-width: 0; min-height: 0; }
.field-error { display: block; color: #a32a28; font-size: 12px; white-space: normal; }
.user-result { max-width: 220px; font-size: 12px; }
.user-tools { display: grid; grid-template-columns: minmax(190px, 1fr) repeat(2, minmax(150px, 220px)); gap: 10px; margin-bottom: 18px; }
.users-table [hidden] { display: none; }
.user-actions { display: grid; gap: 7px; }
.user-actions button { min-width: 100px; }
.reward-history { margin-top: 16px; }
.profile-details { display: flex; gap: 20px; flex-wrap: wrap; }
.profile-details dt { font-weight: 650; }
.profile-details dd { margin: 4px 0 16px; }
.profile-meta { color: var(--muted); font-size: 13px; }
.user-create { margin-top: 24px; }
.user-create .form-grid { grid-template-columns: repeat(auto-fit, minmax(min(210px, 100%), 1fr)); }
.user-create button { max-width: 220px; }
.user-dirty { box-shadow: inset 3px 0 var(--amber); }
.user-incomplete { background: #fff8e9; }
.reward-form { max-width: 650px; }
.profile-orders { max-height: 480px; overflow: auto; }

.login-page { display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at top, #edf3ff, var(--wash) 55%); }
.login-page main { width: min(100%, 430px); margin: 0; padding: 0; }
.login { width: 100%; margin: 0; padding: 30px; border: 1px solid var(--line); border-radius: 22px; background: #fff; box-shadow: 0 20px 55px rgb(18 35 60 / 13%); }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; font-size: 21px; }
.login h1 { margin-bottom: 9px; font-size: 28px; }
.login p { margin: 0 0 22px; }
.login label { display: grid; gap: 5px; color: #4e5d72; font-size: 13px; font-weight: 650; }

@media (max-width: 1099px) {
  .app-bar {
    position: fixed;
    z-index: 70;
    top: 0;
    right: 0;
    left: 0;
    height: calc(var(--app-bar-height) + var(--tg-safe-area-top));
    padding: var(--tg-safe-area-top) max(8px, var(--tg-safe-area-right)) 0 max(8px, var(--tg-safe-area-left));
    display: flex;
    align-items: center;
    gap: 4px;
    border-bottom: 1px solid rgb(198 210 227 / 78%);
    background: rgb(244 247 252 / 90%);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }
  .app-brand { height: 44px; padding: 0 8px; color: var(--navy); font-size: 18px; }
  .app-brand img { width: 27px; height: 27px; }
  .nav-toggle, .nav-close { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border-radius: 12px; background: transparent; color: var(--navy); }
  .nav-toggle:hover, .nav-close:hover { background: #e5ebf5; }
  .nav-toggle span, .nav-toggle span::before, .nav-toggle span::after { display: block; width: 21px; height: 2px; border-radius: 2px; background: currentColor; }
  .nav-toggle span { position: relative; }
  .nav-toggle span::before, .nav-toggle span::after { content: ""; position: absolute; left: 0; }
  .nav-toggle span::before { top: -7px; }
  .nav-toggle span::after { top: 7px; }
  .nav-backdrop { position: fixed; z-index: 115; inset: 0; background: rgb(4 16 34 / 45%); backdrop-filter: blur(1px); }
  #main-nav {
    z-index: 120;
    inset: 0;
    width: 100%;
    width: 100dvw;
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    padding: calc(16px + var(--tg-safe-area-top)) max(16px, var(--tg-safe-area-right)) max(14px, var(--tg-safe-area-bottom)) max(16px, var(--tg-safe-area-left));
    border-radius: 0;
    transform: translateX(-100%);
    visibility: hidden;
    transition: none;
    box-shadow: none;
  }
  #main-nav.open { transform: translateX(0); visibility: visible; transition: transform .22s ease; }
  #main-nav .brand { padding-right: 52px; }
  .nav-close { position: absolute; top: calc(10px + var(--tg-safe-area-top)); right: 10px; color: #dbe6f7; font-size: 30px; font-weight: 400; line-height: 1; }
  .nav-close:hover { background: var(--navy-raised); color: #fff; }
  .menu { padding-right: 2px; }
  main {
    width: 100%;
    max-width: none;
    margin: 0;
    padding: calc(var(--app-bar-height) + var(--tg-safe-area-top) + 24px) max(24px, var(--tg-safe-area-right)) calc(48px + var(--tg-safe-area-bottom)) max(24px, var(--tg-safe-area-left));
  }
  .login-page main { padding: 0; }
  .advanced-filters .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .advanced-filters .wide { grid-column: 1 / -1; }
  .user-tools { grid-template-columns: minmax(180px, 1fr) repeat(2, minmax(145px, 210px)); }
}

@media (max-width: 700px) {
  main { padding-top: calc(var(--app-bar-height) + var(--tg-safe-area-top) + 18px); padding-right: max(14px, var(--tg-safe-area-right)); padding-left: max(14px, var(--tg-safe-area-left)); }
  h1 { margin-bottom: 17px; font-size: 27px; }
  h2 { margin-top: 25px; }
  .panel, .card { padding: 16px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .metric b { font-size: 22px; }
  .form-grid, .advanced-filters .form-grid { grid-template-columns: 1fr; }
  .form-grid .wide, .advanced-filters .wide { grid-column: auto; }
  .page-filter { display: block; padding: 10px; }
  .filter-presets { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
  .filter-preset { width: 100% !important; padding: 7px 5px; font-size: 13px; }
  .filter-custom { display: block; margin-top: 8px; }
  .filter-custom summary { display: flex; align-items: center; width: 100%; color: var(--blue-dark); }
  .filter-custom[open] summary { margin-bottom: 9px; }
  .filter-custom-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .filter-custom-fields label { min-width: 0; }
  .filter-custom-fields input, .filter-custom-fields button { width: 100%; }
  .filter-custom-fields button { grid-column: 1 / -1; }
  .advanced-filters { padding: 10px 12px; }
  .order-list { overflow: visible; }
  .order-list table, .order-list tbody, .order-list tr, .order-list td { display: block; width: 100%; }
  .order-list table { border: 0; background: transparent; }
  .order-list thead { display: none; }
  .order-list tr { margin-bottom: 10px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 14px; background: #fff; box-shadow: 0 2px 5px rgb(18 36 58 / 4%); }
  .order-list td { display: grid; grid-template-columns: minmax(94px, 38%) minmax(0, 1fr); gap: 10px; min-height: 35px; padding: 7px 0; border-bottom: 1px solid #edf1f7; }
  .order-list td:last-child { min-width: 0; }
  .order-list td:last-child { border-bottom: 0; }
  .order-list td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 700; }
  .order-list .cell-value { min-width: 0; overflow-wrap: anywhere; }
  .profile-details { display: grid !important; grid-template-columns: 1fr; gap: 0 !important; }
  .user-tools { grid-template-columns: 1fr; }
  .user-tools input, .user-tools select { max-width: none; }
  .user-create button { max-width: none; }
  dialog { max-width: calc(100% - 24px) !important; padding: 18px !important; }
}

@media (max-width: 420px) {
  .app-brand b { display: none; }
  .grid { grid-template-columns: 1fr; }
  .filter-custom-fields { grid-template-columns: 1fr; }
  .filter-custom-fields button { grid-column: auto; }
  .login-page { padding: 14px; }
  .login { padding: 24px 20px; }
}

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