/* Theme tokens
   Items should contrast with the page: lighter rows on dark, darker rows on light.
   Type is shown via subtle row background tints (--row-currency etc.), not text color. */
:root,
[data-theme="dark"] {
  --bg: #080b10;
  --bg-elevated: #121820;
  --bg-card: #1c2430; /* lighter than page for contrast */
  --bg-hover: #263040;
  --border: #2a3545;
  --border-strong: #3d4d63;
  --text: #e8eef6;
  --text-muted: #8b9bb0;
  --text-dim: #5c6b7f;
  --accent: #3d9cf0;
  --accent-soft: rgba(61, 156, 240, 0.16);
  --accent-hover: #5aadf5;
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --currency: #5eb0ff;
  --crypto: #c4a1ff;
  --stock: #3ee0a0;
  /* Subtle type fills on dark (lifted + tinted) */
  --row-currency: #1a2838;
  --row-crypto: #241f36;
  --row-stock: #172a28;
  --row-border-currency: #2a3f55;
  --row-border-crypto: #3a3255;
  --row-border-stock: #2a4540;
  --chip-bg: #243040;
  --input-bg: #121820;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  --backdrop: rgba(0, 0, 0, 0.55);
}

[data-theme="light"] {
  --bg: #eef1f5;
  --bg-elevated: #ffffff;
  --bg-card: #dce2ea; /* darker than page for contrast */
  --bg-hover: #cfd7e2;
  --border: #c2ccd8;
  --border-strong: #9aabc0;
  --text: #0f172a;
  --text-muted: #4b5c70;
  --text-dim: #7a8a9c;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --accent-hover: #1d4ed8;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --currency: #1d4ed8;
  --crypto: #6d28d9;
  --stock: #047857;
  /* Subtle type fills on light (deeper + tinted) */
  --row-currency: #d0dcea;
  --row-crypto: #ddd4ea;
  --row-stock: #cfe0d8;
  --row-border-currency: #b0c0d4;
  --row-border-crypto: #c0b4d4;
  --row-border-stock: #a8c4b4;
  --chip-bg: #d4dbe5;
  --input-bg: #e8edf3;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.1);
  --backdrop: rgba(15, 23, 42, 0.35);
}

/* Pastel: fully distinct palette — peach/lavender surfaces, soft colored chrome */
[data-theme="pastel"] {
  --bg: #f3e6f4;
  --bg-elevated: #fff5eb;
  --bg-card: #e8d4ec; /* deeper than page, lavender-tinted */
  --bg-hover: #dfc8e6;
  --border: #d4b4d8;
  --border-strong: #c49bc8;
  --text: #4a2c55;
  --text-muted: #8a6a92;
  --text-dim: #b090b0;
  --accent: #e879a9;
  --accent-soft: rgba(232, 121, 169, 0.2);
  --accent-hover: #db5f96;
  --success: #5ecf9a;
  --warning: #f0b45a;
  --danger: #e87898;
  --currency: #5b9fd4;
  --crypto: #b07ad4;
  --stock: #5cba8a;
  --row-currency: #d6e4f2;
  --row-crypto: #e6d8f2;
  --row-stock: #d4ebe0;
  --row-border-currency: #b8cfe4;
  --row-border-crypto: #d0bce4;
  --row-border-stock: #b4d8c8;
  --chip-bg: #f0d4e8;
  --input-bg: #faf0f6;
  --shadow: 0 10px 32px rgba(160, 80, 140, 0.14);
  --backdrop: rgba(100, 50, 90, 0.32);
}

:root {
  --radius: 12px;
  --radius-sm: 8px;
  --font: "DM Sans", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  transition: background 0.2s, color 0.2s;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

code {
  font-family: var(--mono);
  font-size: 0.9em;
  color: var(--accent);
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(6px + var(--safe-top)) 10px calc(8px + var(--safe-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Single title line: logo | status | actions — never wraps */
.header {
  flex-shrink: 0;
  margin: 0 0 6px;
  padding: 0;
}

.header-bar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 8px;
  width: 100%;
  height: 36px;
  max-height: 36px;
  overflow: hidden;
}

.logo {
  margin: 0;
  padding: 0;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  background: var(--bg-hover);
  color: var(--text);
  outline: none;
}

.icon-btn.spinning svg {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.status-line {
  display: block;
  margin: 0;
  padding: 0;
  min-width: 0;
  max-width: 100%;
  font-size: 0.65rem;
  font-weight: 500;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 36px;
  height: 36px;
}

.status-line.offline { color: var(--warning); }
.status-line.error { color: var(--danger); }

.assets-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* Compact asset list */
.asset-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.asset-row {
  display: grid;
  grid-template-columns: 28px minmax(3.2rem, 4.8rem) 1fr;
  align-items: center;
  gap: 4px 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px 8px 4px;
  min-height: 44px;
  transition: border-color 0.12s, background 0.12s, opacity 0.12s, transform 0.12s, box-shadow 0.12s;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}

/* Type via subtle background tint (not text color) */
.asset-row.type-currency {
  background: var(--row-currency);
  border-color: var(--row-border-currency);
}

.asset-row.type-crypto {
  background: var(--row-crypto);
  border-color: var(--row-border-crypto);
}

.asset-row.type-stock {
  background: var(--row-stock);
  border-color: var(--row-border-stock);
}

.asset-row:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
  user-select: text;
  -webkit-user-select: text;
}

.asset-row.source {
  border-color: var(--accent);
}

.asset-row.dragging {
  opacity: 0.55;
  transform: scale(1.02);
  box-shadow: var(--shadow);
  z-index: 5;
  border-color: var(--accent);
  position: relative;
}

.asset-row.drag-over-above {
  box-shadow: inset 0 2px 0 0 var(--accent);
}

.asset-row.drag-over-below {
  box-shadow: inset 0 -2px 0 0 var(--accent);
}

.drag-handle {
  display: grid;
  place-items: center;
  width: 28px;
  height: 32px;
  color: var(--text-dim);
  cursor: grab;
  touch-action: none;
  border-radius: 6px;
  flex-shrink: 0;
}

.drag-handle:hover,
.drag-handle:focus-visible {
  color: var(--text-muted);
  background: var(--bg-hover);
  outline: none;
}

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

.drag-handle svg {
  pointer-events: none;
}

.asset-code {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
  line-height: 1.15;
  min-width: 0;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.asset-input {
  width: 100%;
  min-width: 0;
  background: var(--input-bg);
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
  font-family: var(--mono);
  font-size: 0.95rem;
  font-weight: 500;
  letter-spacing: -0.02em;
  text-align: right;
  color: var(--text);
  transition: border-color 0.12s, background 0.12s;
}

.asset-input:focus {
  outline: none;
  border-color: var(--border-strong);
}

.asset-input.invalid {
  border-color: var(--danger);
  color: var(--danger);
}

.hint {
  margin: 0;
  font-size: 0.72rem;
  color: var(--text-dim);
}

.empty-state {
  text-align: center;
  padding: 28px 14px;
  color: var(--text-muted);
  font-size: 0.85rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* Pull-to-refresh */
.ptr {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  transition: none;
  color: var(--text-muted);
  pointer-events: none;
}

.ptr.visible {
  opacity: 1;
}

.ptr.refreshing .ptr-spinner {
  animation: spin 0.8s linear infinite;
}

.ptr-spinner {
  width: 22px;
  height: 22px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
}

.ptr-label {
  font-size: 0.72rem;
  font-weight: 500;
}

/* —— Top-anchored sheet (keyboard-safe) —— */
.sheet {
  width: min(100%, 480px);
  max-height: var(--sheet-max-h, 100dvh);
  padding: 0;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}

[data-theme="pastel"] .sheet {
  background: linear-gradient(180deg, #fff8f0 0%, #f8ecf6 100%);
  border-color: var(--border);
}

[data-theme="pastel"] .sheet-sticky {
  background: linear-gradient(180deg, #fff8f0 0%, #faf0f6 100%);
  border-bottom-color: var(--border);
}

[data-theme="pastel"] .tabs {
  background: #edd8ec;
}

[data-theme="pastel"] .tab.active {
  background: #fff5eb;
  color: var(--text);
}

.sheet-top {
  margin: 0 auto auto;
  border-radius: 0 0 16px 16px;
  border-top: none;
}

.sheet::backdrop {
  background: var(--backdrop);
  backdrop-filter: blur(4px);
}

.sheet-inner {
  display: flex;
  flex-direction: column;
  max-height: var(--sheet-max-h, 100dvh);
  height: 100%;
  min-height: 0;
}

.sheet-sticky {
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 12px 12px 0;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sheet-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

/* Selected chips */
.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
  min-height: 28px;
  max-height: 88px;
  overflow-y: auto;
}

.chip-row:empty::after {
  content: "None selected — pick below";
  font-size: 0.75rem;
  color: var(--text-dim);
  align-self: center;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px 4px 8px;
  border-radius: 999px;
  background: var(--chip-bg);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text);
  transition: background 0.12s, border-color 0.12s, opacity 0.12s;
  touch-action: none;
  cursor: grab;
}

.chip:hover,
.chip:focus-visible {
  border-color: var(--border-strong);
  outline: none;
}

.chip.dragging {
  opacity: 0.45;
  cursor: grabbing;
}

.chip.drag-over {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.chip .chip-x {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  margin-left: 2px;
  border-radius: 50%;
  font-size: 0.9rem;
  line-height: 1;
  color: var(--text-dim);
  cursor: pointer;
  touch-action: manipulation;
}

.chip .chip-x:hover {
  color: var(--danger);
  background: rgba(248, 113, 113, 0.12);
}

/* Chips: subtle type background (match list rows) */
.chip.type-currency {
  background: var(--row-currency);
  border-color: var(--row-border-currency);
}

.chip.type-crypto {
  background: var(--row-crypto);
  border-color: var(--row-border-crypto);
}

.chip.type-stock {
  background: var(--row-stock);
  border-color: var(--row-border-stock);
}

.tabs {
  display: flex;
  gap: 3px;
  margin-bottom: 8px;
  padding: 3px;
  background: var(--bg);
  border-radius: 10px;
}

.tab {
  flex: 1;
  padding: 7px 4px;
  border-radius: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}

.tab.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

.search-input {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  margin-bottom: 8px;
  font-size: 0.9rem;
  transition: border-color 0.15s;
}

.search-input:focus {
  outline: none;
  border-color: var(--accent);
}

.sheet-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 8px calc(12px + var(--safe-bottom));
  list-style: none;
  margin: 0;
}

.picker-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.picker-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 10px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.12s;
}

.picker-item:hover,
.picker-item:focus-visible {
  background: var(--bg-hover);
  outline: none;
}

.picker-item.selected {
  background: var(--accent-soft);
}

.picker-item .code {
  font-family: var(--mono);
  font-weight: 700;
  min-width: 3.2rem;
  font-size: 0.9rem;
  color: var(--text);
}

.picker-item .name {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.picker-item .check {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1.5px solid var(--border-strong);
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 0.7rem;
  color: transparent;
}

.picker-item.selected .check {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.stock-custom {
  padding-bottom: 8px;
}

.stock-custom .hint {
  margin-bottom: 6px;
}

.stock-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.stock-row .search-input {
  margin-bottom: 0;
  flex: 1;
}

.primary-btn {
  flex-shrink: 0;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 0.85rem;
  transition: background 0.15s;
}

.primary-btn:hover {
  background: var(--accent-hover);
}

.primary-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.error-text {
  margin: 6px 0 0;
  font-size: 0.75rem;
  color: var(--danger);
}

@media (min-width: 520px) {
  .sheet-top {
    margin: 12px auto auto;
    border-radius: 14px;
    border-top: 1px solid var(--border);
  }
}
