:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #d9e0ea;
  --green: #16a34a;
  --green-dark: #15803d;
  --blue: #2563eb;
  --amber: #d97706;
  --red: #dc2626;
  --shadow: 0 16px 40px rgba(31, 41, 55, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, "Microsoft YaHei", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  place-items: center;
  padding: 22px 22px 84px;
  background: rgba(245, 247, 251, 0.92);
}

.login-screen.is-visible {
  display: grid;
}

.login-card {
  display: grid;
  width: min(420px, 100%);
  gap: 16px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.brand-block {
  display: flex;
  gap: 12px;
  align-items: center;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 8px;
  background: #e8f5ee;
}

.brand-mark span {
  width: 20px;
  height: 20px;
  border-radius: 5px;
  background: var(--green);
}

.brand-block h1,
.brand-block p,
.topbar h1,
.topbar p,
.section-head h2,
.section-head p {
  margin: 0;
}

.brand-block h1,
.topbar h1,
.section-head h2 {
  font-size: 22px;
}

.brand-block p,
.topbar p,
.section-head p,
.hint {
  color: var(--muted);
}

.field-label,
.eyebrow {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.eyebrow {
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0;
}

.control {
  width: 100%;
  min-height: 42px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  outline: none;
}

.control:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.14);
}

.textarea {
  resize: vertical;
  line-height: 1.55;
}

.primary-button,
.ghost-button,
.danger-button,
.icon-button,
.segment {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border-radius: 8px;
  font-weight: 700;
  text-decoration: none;
}

.primary-button {
  border: 1px solid var(--green);
  background: var(--green);
  color: #ffffff;
  padding: 0 16px;
}

.primary-button:hover {
  background: var(--green-dark);
}

.ghost-button {
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--text);
  padding: 0 14px;
}

.danger-button {
  border: 1px solid rgba(220, 38, 38, 0.25);
  background: #fff1f2;
  color: var(--red);
  padding: 0 14px;
}

.icon-button {
  width: 36px;
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--text);
}

.back-link {
  position: absolute;
  right: 24px;
  bottom: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--green-dark);
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 10px 24px rgba(31, 41, 55, 0.08);
}

.back-link:hover {
  border-color: var(--green);
  background: #f0fdf4;
}

.icp-footer {
  position: fixed;
  right: 0;
  bottom: 10px;
  left: 0;
  z-index: 30;
  display: flex;
  justify-content: center;
  padding: 0 16px;
  pointer-events: none;
  font-size: 12px;
}

.icp-footer a {
  color: var(--muted);
  pointer-events: auto;
  text-decoration: none;
}

.icp-footer a:hover {
  color: var(--green-dark);
  text-decoration: underline;
}

.remember-login {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  width: max-content;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}

.remember-login input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--green);
}

.login-error {
  min-height: 20px;
  margin: 0;
  color: var(--red);
  font-size: 13px;
}

.app-shell {
  width: min(1840px, calc(100% - 40px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

.topbar,
.section-head {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
}

.topbar {
  margin-bottom: 22px;
}

.topbar-actions,
.modal-actions,
.pool-toolbar,
.channel-title-row,
.channel-tools,
.add-channel {
  display: flex;
  gap: 10px;
  align-items: center;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric-card,
.panel,
.channel-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.metric-card {
  display: flex;
  min-width: 0;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
}

.metric-card span {
  display: block;
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.metric-card strong {
  display: block;
  flex: 0 0 auto;
  margin-top: 0;
  font-size: 24px;
}

.panel {
  padding: 16px;
}

.channel-title-row {
  gap: 14px;
}

.channel-title-actions {
  display: inline-flex;
  flex-wrap: nowrap;
  gap: 8px;
  align-items: center;
}

.channel-tools {
  justify-content: flex-end;
}

.category-filter-row {
  display: flex;
  gap: 10px;
  align-items: center;
  min-width: 0;
  margin-top: 10px;
}

.channel-select-all {
  display: inline-flex;
  flex: 0 0 auto;
  min-height: 34px;
  align-items: center;
  gap: 7px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}

.channel-select-all:has(input:checked),
.channel-select-all:has(input:indeterminate) {
  border-color: #86c9a0;
  background: #e8f5ee;
  color: var(--green-dark);
}

.channel-select-all input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--green);
}

.category-filter {
  display: flex;
  flex: 1 1 auto;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
  white-space: nowrap;
}

.category-filter::-webkit-scrollbar {
  display: none;
}

.category-tab {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--muted);
  font-size: var(--category-font-size, 13px);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
}

.category-tab.is-active {
  border-color: #86c9a0;
  background: #e8f5ee;
  color: var(--green-dark);
}

.add-channel {
  flex: 0 1 660px;
  width: min(660px, 100%);
  flex-wrap: nowrap;
}

.add-channel .control {
  flex: 1 1 auto;
  min-width: 0;
}

.add-channel .primary-button {
  flex: 0 0 auto;
  min-width: 104px;
  white-space: nowrap;
}

.add-channel select.control {
  flex: 0 1 126px;
  min-width: 92px;
}

.channel-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 12px;
}

.channel-card {
  display: grid;
  gap: 10px;
  padding: 12px;
}

.channel-leading {
  display: flex;
  gap: 10px;
  min-width: 0;
  align-items: flex-start;
}

.channel-leading .channel-head {
  flex: 1 1 auto;
  min-width: 0;
}

.channel-select {
  display: inline-flex;
  flex: 0 0 auto;
  min-height: 34px;
  align-items: center;
  justify-content: center;
}

.channel-select input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--green);
}

.channel-head {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  justify-content: space-between;
}

.channel-title {
  min-width: 0;
}

.channel-head h3 {
  margin: 0;
  overflow: hidden;
  font-size: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.channel-head-actions {
  display: inline-flex;
  flex: 0 0 auto;
  gap: 8px;
  align-items: center;
}

.channel-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  min-width: 0;
}

.channel-stat {
  display: flex;
  min-width: 0;
  min-height: 32px;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 5px 7px;
  border-radius: 8px;
  background: #f8fafc;
  white-space: nowrap;
}

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

.channel-stat strong {
  display: block;
  margin-top: 0;
  font-size: 16px;
}

.channel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

@media (min-width: 1440px) {
  .channel-card {
    grid-template-columns: minmax(220px, 0.8fr) minmax(760px, 2.8fr) auto;
    gap: 12px;
    align-items: center;
    padding: 9px 12px;
  }

  .channel-head {
    align-items: center;
  }

  .channel-leading {
    align-items: center;
  }

  .channel-actions {
    flex-wrap: nowrap;
    justify-content: flex-end;
  }

  .channel-actions .mini-button {
    white-space: nowrap;
  }

  .channel-stats {
    grid-template-columns: repeat(9, minmax(62px, 1fr));
  }

  .channel-stat {
    padding: 5px 6px;
  }
}

@media (min-width: 1840px) {
  .channel-card {
    grid-template-columns: minmax(500px, 1fr) minmax(0, 1.75fr) auto;
    gap: 10px;
  }

  .channel-stats {
    gap: 3px;
  }

  .channel-stat {
    gap: 3px;
    padding: 5px 4px;
  }

  .channel-actions {
    gap: 6px;
  }
}

.mini-button {
  min-height: 32px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  font-weight: 700;
  white-space: nowrap;
}

.suffix-modal-hint {
  margin: -2px 0 0;
  font-size: 13px;
}

.mini-button.primary {
  border-color: rgba(22, 163, 74, 0.3);
  color: var(--green-dark);
}

.mini-button.blue {
  border-color: rgba(37, 99, 235, 0.26);
  color: var(--blue);
}

.mini-button.danger {
  border-color: rgba(220, 38, 38, 0.26);
  color: var(--red);
}

.field-tools {
  display: flex;
  justify-content: flex-end;
  margin: 0 0 8px;
}

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

.modal {
  width: min(760px, calc(100% - 24px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
}

.modal::backdrop {
  background: rgba(15, 23, 42, 0.36);
}

.modal-card {
  display: grid;
  gap: 16px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.modal-card.small {
  max-width: 520px;
  margin: 0 auto;
}

.modal-card.wide {
  width: min(1080px, calc(100vw - 24px));
}

#poolModal {
  width: min(1680px, calc(100vw - 64px));
}

#poolModal .modal-card.wide {
  width: 100%;
  max-height: 84vh;
}

#poolModal .table-wrap {
  max-height: 64vh;
}

.modal-head {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
}

.modal-head h3 {
  margin: 0;
}

.pool-toolbar {
  flex-wrap: wrap;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
}

.segment {
  min-height: 32px;
  padding: 0 12px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.segment.is-active {
  background: #ffffff;
  color: var(--green-dark);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
}

.search-control {
  width: min(260px, 100%);
}

.table-wrap {
  max-height: 58vh;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.data-table th,
.data-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f8fafc;
  color: var(--muted);
  font-size: 12px;
}

.code-cell {
  max-width: 460px;
  overflow-wrap: anywhere;
  font-family: Consolas, "Courier New", monospace;
}

.status-pill {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.status-pill.unused {
  background: #e8f5ee;
  color: var(--green-dark);
}

.status-pill.used {
  background: #eff6ff;
  color: var(--blue);
}

.status-pill.deleted {
  background: #fff1f2;
  color: var(--red);
}

.draw-result {
  display: grid;
  gap: 12px;
}

.draw-result code {
  min-height: 52px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  color: var(--text);
  overflow-wrap: anywhere;
}

.draw-suffix {
  min-height: 44px;
  margin: 0;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  font-family: Arial, "Microsoft YaHei", sans-serif;
  line-height: 1.55;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.sort-list {
  display: grid;
  gap: 10px;
  max-height: 54vh;
  overflow: auto;
  padding-right: 2px;
}

.sort-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.sort-item.is-dragging {
  opacity: 0.5;
}

.drag-handle {
  display: inline-flex;
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  color: var(--muted);
  cursor: grab;
  font-family: monospace;
  font-size: 16px;
  font-weight: 700;
}

.drag-handle:active {
  cursor: grabbing;
}

.sort-index {
  display: inline-flex;
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: #f8fafc;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.sort-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow-wrap: anywhere;
  font-weight: 700;
}

.sort-actions {
  display: inline-flex;
  flex: 0 0 auto;
  gap: 8px;
}

.category-add-row,
.category-manager-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.category-add-row .control {
  min-width: 0;
}

.category-manager-list {
  display: grid;
  gap: 10px;
  max-height: 46vh;
  overflow: auto;
}

.category-manager-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.category-manager-item .control:disabled {
  color: var(--muted);
  background: #f8fafc;
}

.default-category-badge {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  padding: 0 8px;
  border-radius: 8px;
  background: #e8f5ee;
  color: var(--green-dark);
  font-size: 12px;
  font-weight: 700;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 80;
  max-width: min(360px, calc(100vw - 40px));
  padding: 12px 14px;
  border-radius: 8px;
  background: #172033;
  color: #ffffff;
  opacity: 0;
  transform: translateY(8px);
  transition: 0.18s ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1080px) {
  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 720px) {
  .app-shell {
    width: min(100% - 24px, 1280px);
  }

  .topbar,
  .section-head,
  .channel-title-row,
  .channel-tools,
  .add-channel,
  .topbar-actions {
    align-items: stretch;
    flex-direction: column;
  }

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

  .pool-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .segmented {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .search-control {
    width: 100%;
  }

  .channel-head {
    align-items: stretch;
    flex-direction: column;
  }

  .channel-head-actions,
  .sort-actions {
    width: 100%;
  }

  .channel-head-actions .mini-button,
  .sort-actions .mini-button {
    flex: 1 1 0;
  }

  .sort-item {
    align-items: stretch;
    flex-wrap: wrap;
  }

  .sort-name {
    flex-basis: calc(100% - 92px);
  }

  .category-manager-item {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .category-manager-actions {
    grid-column: 1 / -1;
    width: 100%;
  }

  .category-manager-actions .mini-button {
    flex: 1 1 0;
  }
}

@media (max-width: 720px) {
  body {
    background: #f1f5f2;
  }

  .login-screen {
    padding: 16px 14px calc(76px + env(safe-area-inset-bottom));
  }

  .back-link {
    right: 14px;
    bottom: calc(14px + env(safe-area-inset-bottom));
    min-height: 38px;
    max-width: calc(100vw - 28px);
    padding: 0 12px;
    font-size: 13px;
  }

  .icp-footer {
    bottom: calc(72px + env(safe-area-inset-bottom));
  }

  .app-shell {
    width: 100%;
    padding: 0 12px 28px;
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    align-items: center;
    flex-direction: row;
    min-height: 64px;
    margin: 0 -12px 14px;
    padding: 12px;
    border-bottom: 1px solid var(--line);
    background: rgba(245, 247, 251, 0.95);
    backdrop-filter: blur(14px);
  }

  .topbar h1 {
    font-size: 19px;
  }

  .topbar > div:first-child > p:not(.eyebrow) {
    display: none;
  }

  .topbar-actions {
    flex: 0 0 auto;
    flex-direction: row;
  }

  .topbar-actions .ghost-button {
    min-height: 38px;
    padding: 0 9px;
    font-size: 12px;
  }

  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .metric-card {
    display: block;
    min-height: 98px;
    padding: 14px;
  }

  .metric-card strong {
    margin-top: 12px;
    font-size: 27px;
  }

  .panel {
    padding: 14px;
  }

  .section-head,
  .channel-title-row {
    gap: 10px;
  }

  .channel-tools {
    width: 100%;
    justify-content: flex-start;
  }

  .category-filter-row {
    gap: 8px;
  }

  .category-filter-row .ghost-button {
    flex: 0 0 auto;
    padding: 0 10px;
  }

  .category-filter {
    gap: 4px;
  }

  .category-tab {
    padding: 0 8px;
  }

  .add-channel {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(92px, 0.7fr) auto;
    grid-auto-rows: 36px;
    flex: 0 0 auto;
    min-height: 0;
    width: 100%;
    gap: 7px;
  }

  .add-channel .control,
  .add-channel .primary-button {
    min-height: 36px;
    font-size: 13px;
  }

  .add-channel .control {
    padding: 6px 8px;
  }

  .add-channel .primary-button {
    min-width: 82px;
    padding: 0 9px;
  }

  .add-channel select.control {
    min-width: 0;
  }

  .channel-card {
    padding: 14px;
  }

  .channel-title-actions {
    flex-wrap: wrap;
  }

  .channel-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .channel-actions .mini-button {
    width: 100%;
  }

  .channel-head-actions .mini-button,
  .channel-actions .primary-button,
  .channel-actions .ghost-button {
    min-height: 40px;
  }

  .modal {
    width: 100%;
    max-width: none;
    max-height: 90vh;
    margin: auto 0 0;
    border-radius: 16px 16px 0 0;
  }

  #poolModal {
    width: 100%;
    max-width: none;
  }

  #poolModal .modal-card.wide {
    width: 100%;
    max-height: 88vh;
  }

  #poolModal .table-wrap {
    max-height: 56vh;
  }

  .modal-card {
    max-height: 88vh;
    padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
    overflow-y: auto;
  }

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