:root {
  --accent: #0b6bcb;
  --accent-2: #0a5aab;
  --green: #13a35a;
  --red: #e0383e;
  --bg: #eef2f7;
  --panel: #ffffff;
  --text: #17202b;
  --muted: #667385;
  --line: #e3e8ef;
  --key: #f3f6fa;
  --key-hover: #e6ecf4;
  --shadow: 0 10px 40px rgba(16, 38, 72, .12);
  --radius: 18px;
  font-family: "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1219;
    --panel: #161d27;
    --text: #e8edf3;
    --muted: #8d9aab;
    --line: #253041;
    --key: #1e2734;
    --key-hover: #263142;
    --shadow: 0 10px 40px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; fill: currentColor; flex: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--red); min-height: 1.3em; font-size: .9rem; margin: .6rem 0 0; }

/* ---------- 共用元件 ---------- */
.btn {
  border: 1px solid var(--line); background: var(--panel);
  padding: .6rem 1.1rem; border-radius: 12px; font-weight: 600;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--key); }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--red); }
.btn.small { padding: .35rem .8rem; font-size: .9rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }
.link-btn { background: none; border: 0; color: var(--accent); font-weight: 600; padding: .3rem .4rem; }
.link-btn.confirm { color: var(--red); }

.field { display: block; margin-bottom: 1rem; }
.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .35rem; }
.field input, .field select, .search {
  width: 100%; padding: .7rem .85rem; border-radius: 12px;
  border: 1px solid var(--line); background: var(--key); outline: none;
}
.field input:focus, .field select:focus, .search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(11, 107, 203, .18); }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; font-size: .95rem;
  color: #fff; background: linear-gradient(135deg, #3a8ee6, #0b6bcb);
}
.avatar.big { width: 104px; height: 104px; font-size: 2.1rem; }

/* ---------- 版面 ---------- */
.screen { min-height: 100%; }
.app {
  max-width: 440px; margin: 0 auto; height: 100dvh;
  display: flex; flex-direction: column;
  background: var(--panel);
}
@media (min-width: 520px) {
  body { display: grid; place-items: center; padding: 24px; }
  .app { height: min(860px, calc(100dvh - 48px)); border-radius: 24px; box-shadow: var(--shadow); overflow: hidden; width: 100%; }
}

/* ---------- 登入 ---------- */
.login { display: grid; place-items: center; padding: 24px; width: 100%; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border-radius: 24px;
  padding: 2.2rem 1.8rem 1.6rem; box-shadow: var(--shadow); text-align: center;
}
.login-card h1 { margin: .8rem 0 .2rem; font-size: 1.6rem; }
.login-card p.muted { margin: 0 0 1.6rem; }
.login-card form { text-align: left; }
.brand-mark {
  width: 64px; height: 64px; border-radius: 20px; margin: 0 auto;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, #3a8ee6, #0b6bcb);
}
.brand-mark svg { width: 32px; height: 32px; }

/* ---------- 頂部 ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 18px 12px;
  border-bottom: 1px solid var(--line);
}
.me { display: flex; align-items: center; gap: 12px; }
.me-name { font-weight: 700; }
.presence { display: flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); }
.presence .dot { width: 9px; height: 9px; border-radius: 50%; background: #a0a9b5; }
.presence[data-state="online"] .dot { background: var(--green); box-shadow: 0 0 0 3px rgba(19, 163, 90, .18); }
.presence[data-state="connecting"] .dot { background: #f0a020; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- 分頁內容 ---------- */
.tabs-body { flex: 1; overflow-y: auto; position: relative; }
.tab { display: none; padding: 14px 18px 24px; }
.tab.active { display: block; }
#tab-dial.active { display: flex; flex-direction: column; justify-content: flex-end; min-height: 100%; padding-bottom: 18px; }
.tab-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.tab-head h2 { margin: 0; font-size: 1.3rem; }

/* 撥號 */
.display { text-align: center; padding: 8px 0 14px; }
.number-input {
  width: 100%; border: 0; background: transparent; outline: none;
  text-align: center; font-size: 2rem; font-weight: 600; letter-spacing: .02em;
  padding: .2rem 0;
}
.number-input::placeholder { color: var(--muted); font-weight: 400; font-size: 1.4rem; }
.number-hint { color: var(--muted); font-size: .9rem; min-height: 1.3em; }
.number-hint.bad { color: var(--red); }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; max-width: 300px; margin: 0 auto; width: 100%; }
.key {
  aspect-ratio: 1.35; border: 0; border-radius: 22px; background: var(--key);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  user-select: none; touch-action: manipulation; transition: background .1s, transform .05s;
}
.key:hover { background: var(--key-hover); }
.key:active { transform: scale(.95); }
.key .k { font-size: 1.65rem; font-weight: 500; line-height: 1; }
.key .sub { font-size: .62rem; letter-spacing: .14em; color: var(--muted); margin-top: 3px; }

.dial-actions {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px;
  max-width: 300px; margin: 16px auto 0; width: 100%; justify-items: center; align-items: center;
}
.round {
  width: 68px; height: 68px; border-radius: 50%; border: 0;
  display: grid; place-items: center; color: #fff;
  transition: transform .08s, filter .15s;
}
.round:active { transform: scale(.94); }
.round svg { width: 30px; height: 30px; }
.round.call { background: var(--green); box-shadow: 0 6px 18px rgba(19, 163, 90, .35); }
.round.hangup { background: var(--red); box-shadow: 0 6px 18px rgba(224, 56, 62, .35); }
.round.ghost { background: transparent; color: var(--muted); width: 56px; height: 56px; }
.round.ghost svg { width: 26px; height: 26px; }
.round:hover { filter: brightness(1.06); }

/* 清單 */
.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 10px 4px;
  border-bottom: 1px solid var(--line);
}
.item-main { flex: 1; min-width: 0; cursor: pointer; }
.item-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item.missed .item-title { color: var(--red); }
.item-sub { font-size: .82rem; color: var(--muted); display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.dir svg { width: 15px; height: 15px; vertical-align: -3px; }
.dir-out { color: var(--accent); }
.dir-in { color: var(--green); }
.dir-missed { color: var(--red); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent;
  display: grid; place-items: center; color: var(--muted);
}
.icon-btn:hover { background: var(--key); }
.icon-btn svg { width: 21px; height: 21px; }
.icon-btn.call-mini { color: var(--green); }
.icon-btn.del.confirm { color: #fff; background: var(--red); }
.empty { text-align: center; color: var(--muted); padding: 2.5rem 0; }
.search { margin-bottom: 8px; }

/* 設定 */
.card { border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 14px; }
.row-kv { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 6px 0; }
.row-kv span { color: var(--muted); }

/* 分頁列 */
.tabbar {
  display: grid; grid-template-columns: repeat(4, 1fr);
  border-top: 1px solid var(--line); background: var(--panel);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbtn {
  position: relative; border: 0; background: none; padding: 9px 0 8px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: .75rem;
}
.tabbtn svg { width: 23px; height: 23px; }
.tabbtn.active { color: var(--accent); font-weight: 600; }
.badge {
  position: absolute; top: 4px; left: calc(50% + 6px);
  background: var(--red); color: #fff; font-size: .68rem; font-weight: 700;
  min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px;
  display: grid; place-items: center;
}

/* ---------- 通話畫面 ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  padding: calc(56px + env(safe-area-inset-top)) 24px calc(40px + env(safe-area-inset-bottom));
  color: #fff;
  background: radial-gradient(120% 80% at 50% 0%, #1f5fa8 0%, #0d2a4d 55%, #081627 100%);
  animation: fadein .2s ease-out;
}
@keyframes fadein { from { opacity: 0; transform: scale(.985); } }
.call-top { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.call-top .avatar { background: rgba(255, 255, 255, .14); margin-bottom: 14px; }
.call-name { font-size: 1.7rem; font-weight: 700; }
.call-number { opacity: .75; }
.call-status { opacity: .85; font-variant-numeric: tabular-nums; font-size: 1.05rem; margin-top: 4px; }
.call-quality { margin-top: 8px; background: rgba(240, 160, 32, .25); color: #ffd48a; padding: 4px 12px; border-radius: 999px; font-size: .85rem; }
.overlay.dtmf-open .call-top .avatar { width: 56px; height: 56px; font-size: 1.2rem; margin-bottom: 4px; }
.overlay.dtmf-open .call-name { font-size: 1.25rem; }

.call-dtmf { width: 100%; max-width: 300px; }
.dtmf-digits { text-align: center; font-size: 1.6rem; letter-spacing: .1em; min-height: 2.2rem; margin-bottom: 10px; }
.keypad.dark .key { background: rgba(255, 255, 255, .1); color: #fff; }
.keypad.dark .key:hover { background: rgba(255, 255, 255, .18); }
.keypad.dark .key .sub { color: rgba(255, 255, 255, .55); }

.call-controls { display: flex; align-items: center; justify-content: center; gap: 28px; }
.ctrl {
  border: 0; background: none; color: #fff; font-size: .8rem;
  display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 64px;
}
.ctrl-icon {
  width: 60px; height: 60px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(255, 255, 255, .14); transition: background .15s;
}
.ctrl.on .ctrl-icon { background: #fff; color: #0d2a4d; }

/* 來電 */
.incoming-actions { display: flex; gap: 90px; }
.incoming-actions .act { display: flex; flex-direction: column; align-items: center; gap: 8px; font-size: .9rem; }
.ring-wrap { position: relative; }
.ring-wrap::before, .ring-wrap::after {
  content: ""; position: absolute; inset: 0; width: 104px; height: 104px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .35); animation: ring 2s infinite ease-out;
}
.ring-wrap::after { animation-delay: 1s; }
@keyframes ring { from { transform: scale(1); opacity: 1; } to { transform: scale(1.7); opacity: 0; } }
.incoming #btn-accept { animation: wiggle 1.4s infinite; }
@keyframes wiggle { 0%, 60%, 100% { transform: rotate(0); } 70% { transform: rotate(-12deg); } 80% { transform: rotate(12deg); } 90% { transform: rotate(-8deg); } }

/* ---------- 對話框 / 提示 ---------- */
dialog {
  border: 0; border-radius: 20px; padding: 1.4rem; width: min(92vw, 380px);
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(8, 16, 28, .5); }
dialog h3 { margin: 0 0 1rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: .4rem; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: rgba(20, 28, 40, .94); color: #fff; padding: .7rem 1.1rem; border-radius: 12px;
  max-width: min(90vw, 400px); text-align: center; font-size: .92rem;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
