:root {
  --bg: #0c1118;
  --panel: #141c26;
  --panel-2: #1c2734;
  --text: #eef3f8;
  --muted: #8fa0b3;
  --accent: #3aa0e8;
  --accent-2: #2580c7;
  --good: #3dcb8c;
  --bad: #ef6b74;
  --warn: #e2b75a;
  --line: #2a3646;
  --radius: 14px;
  --pad: 28px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  height: 100%;
}
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  background:
    radial-gradient(1000px 520px at 8% -8%, #16324a 0%, transparent 55%),
    radial-gradient(800px 480px at 100% 0%, #14281f 0%, transparent 50%),
    var(--bg);
  color: var(--text);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
#app {
  width: 100%;
  max-width: 100%;
}
button, .btn, .nav-btn, .tab {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  -webkit-appearance: none;
  appearance: none;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--bad); min-height: 1.2em; margin: 8px 0 0; }

.auth-panel {
  max-width: 420px;
  margin: 12vh auto;
  padding: 32px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 18px 50px rgba(0,0,0,.35);
}
.auth-panel h1 { margin: 0 0 6px; font-size: 24px; letter-spacing: .02em; }
.tabs { display: flex; gap: 8px; margin: 18px 0 14px; }
.tab {
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
}
.tab.active { color: var(--text); border-color: var(--accent); }

label {
  display: block;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--muted);
}
input, select, textarea {
  display: block;
  width: 100%;
  max-width: 100%;
  margin-top: 6px;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #0f151d;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  line-height: 1.3;
}
textarea { resize: vertical; }
input[type="checkbox"] {
  width: auto;
  margin: 0;
  -webkit-appearance: checkbox;
  appearance: auto;
}
.check {
  display: flex;
  align-items: center;
  gap: 8px;
}
.check input { width: auto; margin: 0; }

.btn {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  transition: filter .15s ease, border-color .15s ease;
}
.btn:hover { filter: brightness(1.1); }
.btn.primary { background: var(--accent-2); border-color: var(--accent); }
.btn.danger { background: #3a1a22; border-color: var(--bad); }
.btn.good { background: #163528; border-color: var(--good); }
.btn.sm { padding: 7px 12px; font-size: 12px; }
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  pointer-events: none;
}

.topbar {
  border-bottom: 1px solid var(--line);
  background: rgba(20, 28, 38, 0.96);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 10;
  padding-top: env(safe-area-inset-top, 0px);
}
.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 14px var(--pad);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.quotes { display: flex; gap: 10px; justify-content: center; min-width: 0; }
.quote {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(15, 21, 29, .65);
  font-size: 13px;
  min-width: 0;
}
.quote-symbol { font-weight: 700; letter-spacing: .04em; }
.quote-price { font-weight: 650; font-variant-numeric: tabular-nums; }
.quote-sub { color: var(--muted); font-size: 12px; }
.quote .up { color: var(--good); }
.quote .down { color: var(--bad); }

.nav {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 14px var(--pad) 0;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}
.nav-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 8px 14px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  font: inherit;
}
.nav-btn.active { color: var(--text); border-bottom-color: var(--accent); }
.nav-logout {
  margin-left: 8px;
  color: var(--muted);
}
.nav-logout:hover { color: var(--bad); }

main {
  padding: 20px var(--pad) 48px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
  overflow-x: clip;
}
.row-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.row-between h2 { margin: 0; font-size: 20px; font-weight: 600; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 520px), 1fr));
  gap: 20px;
}
.card {
  background: linear-gradient(165deg, #182230 0%, var(--panel) 45%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 24px;
  box-shadow: 0 10px 30px rgba(0,0,0,.22);
  min-width: 0;
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.card-title { min-width: 0; flex: 1; }
.card h3 {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: .01em;
  word-break: break-word;
}
.card-detail {
  margin: 0;
}
.card-detail > summary {
  list-style: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  user-select: none;
  -webkit-user-select: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 0;
}
.card-detail > summary::-webkit-details-marker { display: none; }
.card-detail > summary::before {
  content: '▸';
  font-size: 11px;
  line-height: 1;
}
.card-detail[open] > summary::before { content: '▾'; }
.card-detail > summary:hover { color: var(--text); }
.card-sub {
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.5;
}

.kv {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  gap: 8px 14px;
  font-size: 13px;
  line-height: 1.55;
  margin: 0;
}
.kv dt {
  margin: 0;
  color: var(--muted);
  font-weight: 500;
}
.kv dd {
  margin: 0;
  color: var(--text);
  overflow-wrap: anywhere;
  word-break: break-word;
}
.kv dd strong { font-weight: 650; }
.kv-section {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px dashed var(--line);
}
.kv-section-title {
  font-size: 12px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 10px;
}

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--line);
  white-space: nowrap;
  flex-shrink: 0;
}
.badge.on {
  color: var(--good);
  border-color: rgba(61, 203, 140, .45);
  background: rgba(61, 203, 140, .1);
}
.badge.off {
  color: var(--muted);
  background: rgba(143, 160, 179, .08);
}
.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  max-width: 100%;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th, td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
th { color: var(--muted); font-weight: 600; background: var(--panel-2); }
.status-success { color: var(--good); }
.pnl-pct { font-weight: 500; font-variant-numeric: tabular-nums; opacity: 0.9; }
.status-failed { color: var(--bad); }
.status-pending { color: var(--warn); }

/* iOS WebKit dialog: 不用依赖 margin:auto 居中 */
dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(520px, calc(100% - 24px));
  max-width: calc(100vw - 24px);
  max-height: min(88vh, 88dvh);
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-sizing: border-box;
}
dialog.dlg-sm {
  width: min(360px, calc(100% - 24px));
}
dialog::backdrop { background: rgba(0,0,0,.55); }
.dlg-form { padding: 22px; }
dialog.dlg-sm .dlg-form { padding: 16px 18px; }
dialog.dlg-sm .dlg-form h3 { margin: 0 0 12px; font-size: 16px; }
dialog.dlg-sm label { margin-bottom: 10px; }
dialog.dlg-sm .dlg-actions { margin-top: 4px; }
.dlg-form h3 { margin: 0 0 14px; word-break: break-word; }
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.buy-quick {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin: -4px 0 10px;
}
.buy-quick .btn { min-height: 36px; }
.daily-detail { margin-top: 20px; }
.daily-detail h3 { margin: 0 0 10px; font-size: 16px; }
.daily-row { cursor: pointer; }
.daily-row:hover td { background: rgba(58, 160, 232, .08); }
.daily-row.row-active td { background: rgba(58, 160, 232, .14); }

.chart-tabs { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.btn.chart-mode.active {
  background: rgba(61, 139, 253, 0.22);
  border-color: var(--accent);
  color: var(--text);
  box-shadow: inset 0 0 0 1px rgba(61, 139, 253, 0.35);
}
.chart-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 16px 12px;
  min-width: 0;
}
.chart-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 14px 22px;
  font-size: 13px;
  margin-bottom: 10px;
  color: var(--muted);
}
.chart-meta strong { color: var(--text); font-weight: 650; }
.chart-box {
  position: relative;
  height: min(72vh, 720px);
  min-height: 560px;
  background: #0f151d;
  border-radius: 10px;
  overflow: hidden;
  touch-action: none;
  cursor: crosshair;
  user-select: none;
}
.chart-box.is-dragging { cursor: grabbing; }
.chart-box canvas { width: 100%; height: 100%; display: block; }
.chart-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
}
.chart-tools .muted { margin: 0; }
.chart-tip {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  background: rgba(20, 28, 38, .94);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text);
  white-space: nowrap;
  max-width: calc(100% - 16px);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 40;
  padding: 12px 18px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(24, 34, 48, .96);
  color: var(--text);
  font-size: 13px;
  box-shadow: 0 12px 32px rgba(0,0,0,.35);
  max-width: min(90vw, 420px);
  width: max-content;
  text-align: center;
  box-sizing: border-box;
}
.toast.ok {
  border-color: rgba(61, 203, 140, .55);
  background: rgba(22, 53, 40, .96);
}
.toast.bad {
  border-color: rgba(239, 107, 116, .55);
  background: rgba(58, 26, 34, .96);
}

@media (max-width: 900px), (hover: none) and (pointer: coarse) {
  :root { --pad: 12px; }
  .auth-panel {
    width: auto;
    max-width: none;
    margin: max(24px, env(safe-area-inset-top, 0px)) 12px 24px;
    padding: 22px 16px;
  }
  .cards {
    grid-template-columns: 1fr;
    gap: 14px;
  }
  main {
    padding-top: 14px;
    padding-bottom: calc(28px + env(safe-area-inset-bottom, 0px));
  }
  .topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    padding-top: env(safe-area-inset-top, 0px);
    background: rgba(12, 17, 24, 0.96);
    -webkit-backdrop-filter: blur(14px);
    backdrop-filter: blur(14px);
    box-shadow: 0 8px 24px rgba(0,0,0,.35);
  }
  .topbar-inner {
    justify-content: stretch;
    gap: 6px;
    padding-top: 8px;
    padding-bottom: 8px;
  }
  .quotes {
    width: 100%;
    flex-direction: column;
    gap: 6px;
  }
  .quote {
    width: 100%;
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-areas:
      "sym price"
      "sub sub";
    align-items: baseline;
    column-gap: 10px;
    row-gap: 2px;
    padding: 8px 10px;
  }
  .quote-symbol { grid-area: sym; }
  .quote-price { grid-area: price; justify-self: start; font-size: 16px; }
  .quote-sub { grid-area: sub; width: 100%; font-size: 12px; }
  #view-app {
    padding-top: var(--mobile-topbar-h, 148px);
  }
  .nav {
    justify-content: flex-start;
    gap: 2px;
    padding-top: 6px;
    padding-bottom: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: var(--mobile-topbar-h, 148px);
    z-index: 40;
    background: rgba(12, 17, 24, 0.94);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
  }
  .nav::-webkit-scrollbar { display: none; }
  .nav-btn {
    flex: 0 0 auto;
    padding: 12px 14px;
    font-size: 14px;
    min-height: 44px;
  }
  .row-between {
    flex-wrap: wrap;
    align-items: center;
  }
  .row-between h2 { font-size: 18px; }
  .card { padding: 16px; border-radius: 12px; }
  .card-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .card-actions .btn {
    min-height: 44px;
    font-size: 14px;
    padding: 10px 12px;
    width: 100%;
  }
  .card-actions .btn.good,
  .card-actions .btn.danger {
    font-weight: 650;
  }
  .chart-tabs {
    justify-content: flex-start;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    width: 100%;
    gap: 6px;
    padding-bottom: 2px;
  }
  .chart-tabs::-webkit-scrollbar { display: none; }
  .chart-tabs .btn { flex: 0 0 auto; min-height: 36px; }
  .page .row-between { flex-direction: column; align-items: stretch; }
  .page .row-between .chart-tabs { margin-top: 4px; }
  .chart-panel { padding: 12px; }
  .chart-meta { gap: 8px 12px; font-size: 12px; }
  .chart-box { height: min(70vw, 420px); min-height: 300px; }
  .chart-tip { white-space: normal; }
  .table-wrap {
    margin: 0;
    border-radius: 12px;
  }
  table { font-size: 12px; min-width: 640px; }
  th, td { padding: 10px; }
  dialog,
  dialog.dlg-sm {
    width: calc(100% - 20px);
    max-width: calc(100vw - 20px);
    max-height: calc(100dvh - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  }
  .dlg-form { padding: 18px 16px; }
  dialog.dlg-sm .dlg-form { padding: 16px; }
  .dlg-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  .dlg-actions .btn { min-height: 44px; width: 100%; }
  .toast {
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    width: calc(100% - 24px);
    max-width: calc(100% - 24px);
  }
}

@media (max-width: 640px) {
  .grid-2 { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 72px minmax(0, 1fr); gap: 6px 10px; font-size: 12px; }
  .card-sub { font-size: 12px; }
  .user-label-short {
    max-width: 48vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    vertical-align: bottom;
  }
}


.alert-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: end;
  margin: 12px 0 16px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}
.alert-form label { margin: 0; min-width: 120px; }
.alert-form .check { display: flex; align-items: center; gap: 8px; min-height: 38px; }
.alert-list { display: flex; flex-direction: column; gap: 10px; }
.alert-item {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(15, 21, 29, .55);
}
.alert-item.fired { border-color: rgba(239, 107, 116, .55); }
.alert-item.paused { opacity: 0.7; }
.alert-item .alert-main { font-weight: 650; }
.alert-item .alert-sub { color: var(--muted); font-size: 12px; }
.alert-item .alert-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.alert-sound-btns { display: flex; gap: 8px; flex-wrap: wrap; }
#alert-sound-status.ok { color: var(--good); }
#alert-sound-status.ringing { color: var(--bad); font-weight: 650; }
.alert-ring-bar {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(239, 107, 116, .55);
  background: rgba(40, 12, 16, .96);
  color: #ffd7db;
  box-shadow: 0 10px 28px rgba(0, 0, 0, .35);
}
.alert-ring-bar.hidden { display: none; }
.alert-ring-bar .btn { flex-shrink: 0; }
