:root {
  --bg: #edf4fb;
  --bg-accent: #dcebf8;
  --ink: #152a43;
  --muted: #536b86;
  --brand: #173f73;
  --accent: #c45c26;
  --line: #c5d7e8;
  --card: #f8fbff;
  --panel: #ffffff;
  --archived: #8a9aa3;
  --selected: #e5f2ff;
  --shadow: 0 14px 36px rgba(23, 63, 115, 0.12);
  --shadow-soft: 0 4px 14px rgba(23, 63, 115, 0.09);
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-body: "Avenir Next", "Segoe UI", "Helvetica Neue", sans-serif;
  --radius: 12px;
  --focus: 0 0 0 3px color-mix(in srgb, var(--brand) 35%, transparent);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #f8fbff 0%, transparent 55%),
    linear-gradient(165deg, var(--bg) 0%, var(--bg-accent) 100%);
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, #fff 78%, transparent);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-family: var(--font-display);
  font-size: clamp(1.45rem, 2.2vw, 2rem);
  color: var(--brand);
  letter-spacing: 0.02em;
  text-decoration: none;
}

.top-actions { display: flex; gap: 0.5rem; align-items: center; }

.account-chip {
  color: var(--muted);
  font-size: 0.88rem;
  background: color-mix(in srgb, #fff 70%, transparent);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
}

.hidden { display: none !important; }

.login-view,
.app-view,
.state-view,
.page-simple main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.35rem 1.25rem 3rem;
}

.login-view h1,
.state-view h1 {
  font-family: var(--font-display);
  font-size: 2rem;
  margin: 0 0 0.5rem;
}

.login-view form {
  display: grid;
  gap: 0.75rem;
  max-width: 360px;
  margin-top: 1.25rem;
}

.state-view {
  text-align: left;
}

.loading-msg {
  color: var(--muted);
  font-size: 1.05rem;
  margin: 2rem 0;
}

.detail-empty h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
  margin: 0 0 0.4rem;
  color: var(--ink);
}

label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--muted);
}

input, select, button {
  font: inherit;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

button {
  cursor: pointer;
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
  min-height: 2.5rem;
}

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

button.secondary {
  background: transparent;
  color: var(--brand);
}

.msg { color: var(--muted); margin-top: 1rem; }

.page-head {
  margin-bottom: 1rem;
}

.page-head h1 {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  clip-path: inset(50%);
  border: 0;
  font-family: var(--font-display);
  font-size: clamp(1.6rem, 2.5vw, 2rem);
  color: var(--brand);
}

.page-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1rem;
}

.app-view {
  display: grid;
  grid-template-columns: minmax(220px, 0.8fr) minmax(0, 2.2fr);
  column-gap: 1.5rem;
  align-items: center;
}

.page-head {
  grid-column: 1;
  grid-row: 1;
  margin-bottom: 0;
}

#op-toast,
.wallet {
  grid-column: 1 / -1;
}

.toolbar {
  grid-column: 2;
  grid-row: 1;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto;
  gap: 0.65rem;
  min-width: 0;
  margin-bottom: 0;
}

.toolbar input[type="search"] {
  flex: initial;
  width: 100%;
  min-width: 0;
  min-height: 2.75rem;
  padding-left: 2.5rem;
  border-color: color-mix(in srgb, var(--line) 78%, #fff);
  border-radius: 9px;
  background-color: color-mix(in srgb, #fff 94%, var(--card));
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%23536b86' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 0.8rem center;
  background-size: 1.1rem;
}

.toolbar-filters {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.65rem;
  align-items: center;
  min-width: 0;
}

.toolbar-filters > label:not(.check) {
  display: block;
  min-width: 0;
  font-size: 0;
}

.toolbar-filters select {
  width: auto;
  min-width: 8.75rem;
  min-height: 2.75rem;
  font-size: 0.88rem;
}

.toolbar-filters .check {
  display: inline-flex;
  gap: 0.55rem;
  align-items: center;
  min-height: 2.75rem;
  font-size: 0.84rem;
  white-space: nowrap;
}

.toolbar-filters .check input[type="checkbox"] {
  appearance: none;
  position: relative;
  width: 2.45rem;
  height: 1.4rem;
  flex: 0 0 2.45rem;
  margin: 0;
  padding: 0;
  border: 1px solid color-mix(in srgb, var(--muted) 38%, #fff);
  border-radius: 999px;
  background: color-mix(in srgb, var(--muted) 16%, #fff);
  transition: background-color 160ms ease, border-color 160ms ease;
}

.toolbar-filters .check input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 0.14rem;
  left: 0.14rem;
  width: 1.02rem;
  height: 1.02rem;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(21, 42, 67, 0.22);
  transition: transform 160ms ease;
}

.toolbar-filters .check input[type="checkbox"]:checked {
  border-color: var(--brand);
  background: var(--brand);
}

.toolbar-filters .check input[type="checkbox"]:checked::after {
  transform: translateX(1.02rem);
}

.toolbar-filters .check input[type="checkbox"]:focus-visible {
  box-shadow: var(--focus);
}

@media (prefers-reduced-motion: reduce) {
  .toolbar-filters .check input[type="checkbox"],
  .toolbar-filters .check input[type="checkbox"]::after {
    transition: none;
  }
}
.check { display: flex; align-items: center; gap: 0.4rem; color: var(--ink); }

.toast {
  margin: 0 0 0.85rem;
  padding: 0.55rem 0.8rem;
  border-radius: 8px;
  background: color-mix(in srgb, var(--brand) 10%, #fff);
  border: 1px solid var(--line);
  color: var(--brand);
  font-size: 0.88rem;
}

.toast.is-error {
  background: #fdeeee;
  border-color: #e8b4b4;
  color: #a12828;
}

/* —— Hybrid wallet —— */
.wallet {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(280px, 0.85fr);
  gap: 1rem;
  align-items: start;
  padding: 1rem;
  border: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
  border-radius: 18px;
  background: color-mix(in srgb, var(--panel) 82%, transparent);
  box-shadow: var(--shadow);
}

.list-panel,
.detail-panel {
  background: var(--panel);
  border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  border-radius: var(--radius);
  box-shadow: var(--shadow-soft);
  min-width: 0;
}

.list-panel {
  padding: 0.35rem;
  border-color: transparent;
  background: transparent;
  box-shadow: none;
}

.detail-panel {
  padding: 1rem 1.1rem 1.15rem;
  position: sticky;
  top: 4.5rem;
  max-height: calc(100dvh - 5.5rem);
  overflow: auto;
}

.holder-block {
  margin-bottom: 1.35rem;
}

.holder-block.is-dragging { opacity: 0.55; }
.holder-block.drag-over {
  outline: 2px dashed color-mix(in srgb, var(--brand) 45%, transparent);
  outline-offset: 2px;
  border-radius: 8px;
}

.holder-head {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.35rem;
}

.holder-head h2 {
  font-family: var(--font-display);
  font-size: 1.25rem;
  margin: 0;
  flex: 1;
}

.holder-controls { display: flex; gap: 0.2rem; }

.drag-handle {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 0.35rem 0.4rem;
  min-height: auto;
  cursor: grab;
  letter-spacing: -0.15em;
  font-size: 0.85rem;
  line-height: 1;
}

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

.icon-btn {
  background: transparent;
  color: var(--brand);
  padding: 0.25rem 0.45rem;
  min-height: 2rem;
  border-color: transparent;
}

.category-block h3 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0.85rem 0 0.35rem;
}

.program-list {
  display: grid;
  gap: 0.4rem;
}

.summary-row {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(0, 0.85fr) minmax(0, 0.7fr) minmax(0, 0.75fr) 2.5rem;
  gap: 0.45rem;
  align-items: center;
  width: 100%;
  text-align: left;
  padding: 0.7rem 0.75rem;
  border: 1px solid color-mix(in srgb, var(--line) 65%, transparent);
  border-radius: 10px;
  background: color-mix(in srgb, var(--card) 88%, #fff);
  color: inherit;
  min-height: auto;
  box-shadow: none;
  cursor: pointer;
}

.summary-row.head {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.72rem;
  padding: 0.25rem 0.7rem 0.35rem;
  cursor: default;
  pointer-events: none;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}

.summary-row:not(.head):hover {
  background: color-mix(in srgb, var(--selected) 55%, #fff);
  border-color: color-mix(in srgb, var(--brand) 18%, var(--line));
}

.summary-row.is-selected {
  background: var(--selected);
  border-color: color-mix(in srgb, var(--brand) 28%, var(--line));
  box-shadow: var(--shadow-soft);
}

.summary-row.is-archived {
  color: var(--archived);
}

.summary-main {
  min-width: 0;
}

.summary-main strong,
.summary-main .sub {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.summary-main .sub {
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: 0.1rem;
}

.summary-balance,
.summary-status,
.summary-expiry {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
}

.badge {
  display: inline-block;
  margin-left: 0.35rem;
  font-size: 0.7rem;
  padding: 0.1rem 0.35rem;
  border: 1px solid var(--archived);
  border-radius: 3px;
  color: var(--archived);
  vertical-align: middle;
}

.summary-alliance {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
}

.alliance-tag-logo {
  display: block;
  width: 1.75rem;
  height: 1.4rem;
  object-fit: contain;
  object-position: center;
}

.alliance-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 4.5rem;
  height: 3rem;
  padding: 0;
}

.alliance-detail-logo {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.empty-state {
  padding: 1.5rem 0.75rem;
  color: var(--muted);
}

.empty-state h2 {
  font-family: var(--font-display);
  color: var(--ink);
  font-size: 1.2rem;
  margin: 0 0 0.4rem;
}

.empty-state p { margin: 0 0 0.85rem; }

/* Detail panel */
.detail-empty {
  padding: 1.5rem 0.25rem;
}

.back-btn {
  display: none;
  background: transparent;
  color: var(--brand);
  border: none;
  padding: 0;
  min-height: auto;
  margin-bottom: 0.85rem;
  font-size: 0.92rem;
}

.detail-head {
  display: flex;
  gap: 0.75rem;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 0.85rem;
}

.detail-kicker {
  margin: 0 0 0.2rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.detail-head h2 {
  font-family: var(--font-display);
  margin: 0;
  font-size: 1.45rem;
  color: var(--brand);
}

.detail-program {
  margin: 0.2rem 0 0;
  color: var(--muted);
}

.detail-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
  margin: 1rem 0;
  padding: 0.35rem;
  border-radius: 12px;
  background: var(--card);
  box-shadow: var(--shadow-soft);
}

.detail-stats > div {
  background: transparent;
  border-radius: 0;
  padding: 0.55rem 0.6rem;
  min-width: 0;
}

.detail-stats > div + div { border-left: 1px solid var(--line); }

.detail-stats dt,
.detail-fields dt {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.01em;
  text-transform: none;
}

.detail-stats dd,
.detail-fields dd {
  margin: 0.2rem 0 0;
  font-size: 0.95rem;
  word-break: break-word;
}

.detail-stats dd {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-fields {
  display: grid;
  gap: 0.7rem;
  margin: 0 0 1rem;
}

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.detail-actions button {
  flex: 1 1 auto;
}

.inline-error { margin-bottom: 0.75rem; }

.muted { color: var(--muted); }
.small { font-size: 0.78rem; }

/* Edit dialog */
dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0;
  width: min(1080px, 96vw);
  max-width: 96vw;
  max-height: min(92dvh, 92vh);
  margin: auto;
  overflow: hidden;
  background: #fff;
  box-shadow: var(--shadow);
}

dialog::backdrop { background: rgba(20, 37, 46, 0.45); }

.edit-form {
  display: flex;
  flex-direction: column;
  max-height: min(92dvh, 92vh);
}

.edit-form h2 {
  margin: 0;
  padding: 1.15rem 1.25rem 0.5rem;
  font-family: var(--font-display);
}

.edit-form > .error {
  margin: 0 1.25rem 0.5rem;
}

.edit-body {
  overflow: auto;
  padding: 0.25rem 1.25rem 0.5rem;
  -webkit-overflow-scrolling: touch;
}

.edit-section {
  margin: 0.75rem 0 1rem;
  padding: 0.85rem 0.9rem;
  background: var(--card);
  border-radius: 10px;
  box-shadow: var(--shadow-soft);
  display: grid;
  grid-template-columns: 112px minmax(0, 1fr);
  gap: 0.75rem 1rem;
  align-items: start;
}

.edit-section h3 {
  margin: 0.2rem 0 0;
  font-size: 0.95rem;
  color: var(--brand);
}

.edit-form .grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
}

.edit-form .is-disabled {
  opacity: 0.55;
}

.dialog-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  padding: 0.85rem 1.25rem calc(0.85rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: #fff;
  position: sticky;
  bottom: 0;
}

.error {
  color: #a12828;
  background: #fdeeee;
  border: 1px solid #e8b4b4;
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 0.88rem;
}

.page-simple h1 { font-family: var(--font-display); }

/* Mobile / portrait: list ↔ detail */
@media (max-width: 900px) and (orientation: portrait), (max-width: 760px) {
  .app-view {
    display: block;
  }

  .app-view[data-pane="detail"] .page-head,
  .app-view[data-pane="detail"] .toolbar {
    display: none;
  }

  .toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.55rem;
    align-items: center;
    margin-bottom: 0.85rem;
  }

  .toolbar input[type="search"] {
    grid-column: 1;
    width: 100%;
    min-width: 0;
    min-height: 2.75rem;
    border-radius: 10px;
  }

  .toolbar-filters {
    grid-column: 1;
    display: flex;
    flex-wrap: nowrap;
    gap: 0.65rem;
    align-items: center;
    min-width: 0;
  }

  .toolbar-filters > label:not(.check) {
    display: block;
    flex: 0 1 auto;
    min-width: 0;
    font-size: 0;
  }

  .toolbar-filters select {
    width: auto;
    min-width: 8.75rem;
    min-height: 2.5rem;
    padding: 0.45rem 2rem 0.45rem 0.7rem;
    border-radius: 8px;
    font-size: 0.88rem;
  }

  .toolbar-filters .check {
    display: inline-flex;
    flex: 0 0 auto;
    gap: 0.55rem;
    min-height: 2.5rem;
    margin-left: auto;
    font-size: 0.84rem;
    white-space: nowrap;
  }

  .wallet {
    grid-template-columns: 1fr;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  .list-panel {
    padding: 0.75rem 0.85rem 1rem;
    border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
    background: var(--panel);
    box-shadow: var(--shadow-soft);
  }

  .detail-panel {
    position: static;
    max-height: none;
    padding-top: 0.75rem;
  }

  .wallet[data-pane="list"] .detail-panel { display: none; }
  .wallet[data-pane="detail"] .list-panel { display: none; }

  .back-btn { display: inline-flex; }

  .summary-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr) 1.75rem;
    grid-template-areas:
      "main main main main"
      "bal status exp alliance";
    row-gap: 0.35rem;
  }

  .summary-row.head { display: none; }

  .summary-main { grid-area: main; }
  .summary-balance { grid-area: bal; font-size: 0.86rem; }
  .summary-status { grid-area: status; font-size: 0.86rem; }
  .summary-expiry {
    grid-area: exp;
    color: var(--muted);
    font-size: 0.82rem;
  }
  .summary-alliance {
    grid-area: alliance;
    justify-content: flex-end;
  }

  .alliance-tag-logo {
    width: 1.45rem;
    height: 1.25rem;
  }

  /* Keep compact 3-up stats on mobile (mockup) */
  .detail-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0;
  }

  .detail-stats > div {
    padding: 0.45rem 0.4rem;
  }

  .detail-stats dt { font-size: 0.7rem; }
  .detail-stats dd { font-size: 0.84rem; }

  .detail-actions {
    flex-direction: column;
  }

  .detail-actions button {
    width: 100%;
  }

  .drag-handle { display: none; }

  .edit-section {
    grid-template-columns: 1fr;
    gap: 0.65rem;
  }

  .edit-section h3 { margin-top: 0; }

  .edit-form .grid { grid-template-columns: 1fr; }

  dialog {
    width: min(100vw - 0.75rem, 560px);
    max-height: min(96dvh, 96vh);
    border-radius: 12px;
  }
}

@media (max-width: 560px) {
  .login-view, .app-view, .state-view {
    padding-left: 1rem;
    padding-right: 1rem;
  }
}

/* —— UI Lab panel (localhost ?ui=lab only) —— */
#ui-lab-panel {
  position: fixed;
  z-index: 1000;
  right: 0.75rem;
  bottom: 0.75rem;
  width: min(340px, calc(100vw - 1.5rem));
  max-height: min(70vh, 560px);
  overflow: auto;
  background: #14252e;
  color: #f3f7f9;
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(20, 37, 46, 0.35);
  padding: 0.75rem 0.85rem 0.9rem;
  font-size: 0.8rem;
}

.ui-lab-head {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  margin-bottom: 0.65rem;
}

.ui-lab-head strong {
  font-family: var(--font-display);
  font-size: 1rem;
}

.ui-lab-head span { opacity: 0.75; }

.ui-lab-section {
  margin-top: 0.65rem;
  padding-top: 0.55rem;
  border-top: 1px solid rgba(255, 255, 255, 0.14);
}

.ui-lab-section-title {
  margin: 0 0 0.4rem;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  opacity: 0.7;
}

.ui-lab-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.ui-lab-actions button {
  background: transparent;
  color: #f3f7f9;
  border: 1px solid rgba(255, 255, 255, 0.28);
  min-height: 2rem;
  padding: 0.3rem 0.5rem;
  font-size: 0.75rem;
}

.ui-lab-actions button.is-active,
.ui-lab-actions button:hover {
  background: #fff;
  color: #14252e;
  border-color: #fff;
}

.ui-lab-custom-vp {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.45rem;
  align-items: center;
  margin-top: 0.5rem;
  color: rgba(243, 247, 249, 0.85);
  font-size: 0.75rem;
}

.ui-lab-custom-vp input {
  min-width: 0;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.ui-lab-hint {
  margin: 0.55rem 0 0;
  opacity: 0.7;
  line-height: 1.35;
}

.ui-lab-hint code {
  font-size: 0.72em;
  opacity: 0.95;
}

html.ui-lab body {
  padding-bottom: 12rem;
}

/* Narrow RWD frame: hide page chrome; show iframe phone viewport */
html.ui-lab-framed body {
  background: #0d1820;
  padding-bottom: 0;
}

html.ui-lab-framed body > *:not(#ui-lab-panel):not(#ui-lab-viewport) {
  display: none !important;
}

#ui-lab-viewport {
  position: fixed;
  z-index: 900;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 390px;
  height: 100vh;
  max-height: 100dvh;
  border: 0;
  border-left: 1px solid rgba(255, 255, 255, 0.12);
  border-right: 1px solid rgba(255, 255, 255, 0.12);
  background: #fff;
  box-shadow: 0 0 0 9999px rgba(8, 14, 18, 0.55);
}

html.ui-lab.ui-lab-framed #ui-lab-panel {
  bottom: 0.75rem;
}
