:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-subtle: #fbfcfe;
  --ink: #17202a;
  --muted: #667085;
  --line: #d8dee8;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --active-bg: #eef4ff;
  --active-line: #b8cdfd;
  --active-ink: #174ea6;
  --input-bg: #ffffff;
  --table-head: #fbfcfe;
  --brand-bg: #111827;
  --brand-ink: #ffffff;
  --green: #16803c;
  --green-soft: #e9f7ee;
  --amber: #9a5b00;
  --amber-soft: #fff4db;
  --red: #b42318;
  --red-soft: #fff0ee;
  --toast-bg: #eef4ff;
  --toast-line: #a7c2f7;
  --toast-ink: #173b78;
  --shadow: 0 12px 32px rgba(17, 24, 39, 0.08);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1117;
  --panel: #171b23;
  --panel-subtle: #11151d;
  --ink: #eef2f7;
  --muted: #9aa4b2;
  --line: #2b3340;
  --accent: #5b8cff;
  --accent-dark: #7aa2ff;
  --active-bg: #182746;
  --active-line: #35558f;
  --active-ink: #c9dcff;
  --input-bg: #0f131a;
  --table-head: #111720;
  --brand-bg: #eef2f7;
  --brand-ink: #111827;
  --green: #8ee6ad;
  --green-soft: #12331f;
  --amber: #ffd37a;
  --amber-soft: #38270e;
  --red: #ffaaa1;
  --red-soft: #401b1a;
  --toast-bg: #152642;
  --toast-line: #35558f;
  --toast-ink: #d7e6ff;
  --shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
.button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--ink);
  background: var(--panel);
  text-decoration: none;
  cursor: pointer;
  transition: 120ms ease;
}

button:hover,
.button:hover {
  border-color: var(--active-line);
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.primary:hover {
  border-color: var(--accent-dark);
  background: var(--accent-dark);
}

.danger {
  color: var(--red);
}

.ghost {
  background: transparent;
}

.center-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(520px, 100%);
  padding: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  margin-bottom: 18px;
  background: var(--brand-bg);
  color: var(--brand-ink);
  font-weight: 800;
}

.login-panel h1 {
  margin: 0 0 10px;
  font-size: 30px;
  letter-spacing: 0;
}

.login-panel p {
  margin: 0 0 18px;
  color: var(--muted);
}

pre {
  overflow: auto;
  padding: 14px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--panel-subtle);
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 3;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.1;
}

.brand span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.userbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.theme-toggle {
  min-width: 74px;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--active-bg);
  font-weight: 700;
}

.layout {
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  min-height: calc(100vh - 64px);
}

.sidebar {
  padding: 18px;
  border-right: 1px solid var(--line);
  background: var(--panel-subtle);
}

.nav-button {
  width: 100%;
  justify-content: flex-start;
  margin-bottom: 8px;
}

.nav-button.active {
  border-color: var(--active-line);
  background: var(--active-bg);
  color: var(--active-ink);
}

.content {
  padding: 22px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.stat {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.stat b {
  display: block;
  font-size: 28px;
  line-height: 1;
}

.stat span {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 6px 18px rgba(17, 24, 39, 0.04);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2 {
  margin: 0;
  font-size: 17px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.search {
  width: min(320px, 100%);
}

input,
select,
textarea {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--input-bg);
  color: var(--ink);
}

input,
select {
  min-height: 38px;
  padding: 0 11px;
}

textarea {
  width: 100%;
  min-height: 190px;
  padding: 12px;
  resize: vertical;
}

.import-body {
  padding: 16px;
}

.table-wrap {
  overflow: auto;
}

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

.codes-table {
  table-layout: fixed;
  min-width: 1120px;
}

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 14px;
}

th {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  background: var(--table-head);
}

tr:last-child td {
  border-bottom: 0;
}

.codes-table th:nth-child(1),
.codes-table td:nth-child(1) {
  width: 28%;
}

.codes-table th:nth-child(2),
.codes-table td:nth-child(2) {
  width: 94px;
}

.codes-table th:nth-child(3),
.codes-table td:nth-child(3),
.codes-table th:nth-child(4),
.codes-table td:nth-child(4) {
  width: 15%;
}

.codes-table th:nth-child(5),
.codes-table td:nth-child(5) {
  width: 110px;
}

.codes-table th:nth-child(6),
.codes-table td:nth-child(6) {
  width: 360px;
}

.code-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  word-break: break-all;
}

.sent-code {
  color: var(--muted);
  text-decoration: line-through;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.available {
  color: var(--green);
  background: var(--green-soft);
}

.reserved {
  color: var(--amber);
  background: var(--amber-soft);
}

.sent {
  color: var(--red);
  background: var(--red-soft);
}

.row-actions,
.send-form {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: nowrap;
  width: 100%;
}

.send-form input {
  flex: 1 1 150px;
  min-width: 0;
  width: auto;
}

.send-form button,
.codes-table td:nth-child(6) > button {
  flex: 0 0 auto;
  white-space: nowrap;
}

.muted {
  color: var(--muted);
}

.settings-body {
  padding: 16px;
}

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

.toggle-row input {
  width: 20px;
  height: 20px;
  min-height: 20px;
  accent-color: var(--accent);
}

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

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  padding: 12px 14px;
  border: 1px solid var(--toast-line);
  border-radius: 8px;
  background: var(--toast-bg);
  color: var(--toast-ink);
  box-shadow: var(--shadow);
  z-index: 10;
}

.login-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (max-width: 860px) {
  .topbar {
    height: auto;
    align-items: flex-start;
    padding: 14px;
    flex-direction: column;
  }

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

  .sidebar {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .nav-button {
    width: auto;
    white-space: nowrap;
    margin: 0;
  }

  .content {
    padding: 14px;
  }

  .stats {
    grid-template-columns: repeat(2, minmax(120px, 1fr));
  }

  .panel-head {
    align-items: flex-start;
    flex-direction: column;
  }
}


.brand .brand-mark {
  width: 38px;
  height: 38px;
  margin-bottom: 0;
  flex: 0 0 auto;
}

@media (max-width: 700px) {
  body {
    background: var(--panel);
  }

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

  .topbar {
    position: static;
    gap: 12px;
  }

  .brand h1 {
    font-size: 16px;
  }

  .brand span {
    font-size: 11px;
  }

  .userbar {
    width: 100%;
    display: grid;
    grid-template-columns: 74px 38px minmax(0, 1fr) auto;
  }

  .userbar strong,
  .userbar span {
    display: inline-block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sidebar {
    padding: 10px 14px;
  }

  .content {
    padding: 12px;
  }

  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
  }

  .stat {
    padding: 12px;
  }

  .stat b {
    font-size: 24px;
  }

  .panel {
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    box-shadow: none;
    margin-left: -12px;
    margin-right: -12px;
  }

  .panel-head {
    padding: 12px;
  }

  .toolbar,
  .login-actions {
    width: 100%;
  }

  .toolbar > *,
  .login-actions > * {
    flex: 1 1 100%;
    width: 100%;
  }

  .search {
    width: 100%;
  }

  .table-wrap {
    overflow: visible;
  }

  table,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
    width: 100% !important;
  }

  table {
    min-width: 0;
  }

  thead {
    position: absolute;
    width: 1px !important;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  tbody {
    display: grid;
    gap: 10px;
    padding: 10px 12px 12px;
  }

  tr {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    overflow: hidden;
  }

  td {
    display: grid;
    grid-template-columns: 98px minmax(0, 1fr);
    gap: 10px;
    min-height: 42px;
    align-items: center;
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
  }

  td::before {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
  }

  .codes-table td:nth-child(1)::before { content: "Code"; }
  .codes-table td:nth-child(2)::before { content: "Status"; }
  .codes-table td:nth-child(3)::before { content: "Claimed"; }
  .codes-table td:nth-child(4)::before { content: "Sent to"; }
  .codes-table td:nth-child(5)::before { content: "Updated"; }
  .codes-table td:nth-child(6)::before { content: "Action"; }

  .users-table td:nth-child(1)::before { content: "User"; }
  .users-table td:nth-child(2)::before { content: "Role"; }
  .users-table td:nth-child(3)::before { content: "Login"; }
  .users-table td:nth-child(4)::before { content: "Action"; }

  .logs-table td:nth-child(1)::before { content: "Time"; }
  .logs-table td:nth-child(2)::before { content: "Action"; }
  .logs-table td:nth-child(3)::before { content: "Actor"; }
  .logs-table td:nth-child(4)::before { content: "Code"; }
  .logs-table td:nth-child(5)::before { content: "Details"; }

  .code-value,
  td code {
    word-break: break-word;
    overflow-wrap: anywhere;
  }

  .send-form {
    flex-wrap: wrap;
  }

  .send-form input {
    flex-basis: 100%;
  }

  .send-form button,
  .codes-table td:nth-child(6) > button {
    flex: 1 1 auto;
  }

  .toast {
    left: 12px;
    right: 12px;
    bottom: 12px;
    max-width: none;
  }
}

@media (max-width: 420px) {
  .stats {
    grid-template-columns: 1fr;
  }

  td {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  td::before {
    margin-bottom: 2px;
  }

  .login-panel {
    padding: 22px;
  }

  .userbar {
    grid-template-columns: 1fr 38px minmax(0, 1fr);
  }

  .userbar .button {
    grid-column: 1 / -1;
  }
}
