/* ===== Design tokens ===== */
:root {
  --bg: #f5f5fb;
  --surface: #ffffff;
  --ink: #1c1a3d;
  --muted: #5c5a7a;
  --line: #e2e0ef;
  --accent: #5446d6;
  --accent-ink: #ffffff;
  --accent-soft: #ecebfd;
  --trace: #c4bdf6;
  --copper: #b06a32;
  --dim: 0.38;
  --shadow: 0 1px 2px rgba(28, 26, 61, 0.05), 0 8px 24px -12px rgba(28, 26, 61, 0.12);

  --font: "IBM Plex Sans", "Noto Sans TC", "Noto Sans CJK TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  --fs-sm: 0.875rem;
  --fs-base: 1rem;
  --fs-md: 1.125rem;
  --fs-lg: 1.375rem;
  --fs-xl: 1.75rem;
  --fs-name: clamp(2.25rem, 4.5vw + 1rem, 3.5rem);

  --gutter: 16px;
  --radius: 16px;
  --radius-sm: 10px;
  --topbar: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121129;
    --surface: #1a1936;
    --ink: #ecebfa;
    --muted: #a8a6c6;
    --line: #2d2b4f;
    --accent: #a49bff;
    --accent-ink: #121129;
    --accent-soft: #27254f;
    --trace: #3b3578;
    --copper: #dc9d62;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px -14px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121129;
  --surface: #1a1936;
  --ink: #ecebfa;
  --muted: #a8a6c6;
  --line: #2d2b4f;
  --accent: #a49bff;
  --accent-ink: #121129;
  --accent-soft: #27254f;
  --trace: #3b3578;
  --copper: #dc9d62;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px -14px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }

html { scroll-behavior: smooth; scroll-padding-top: calc(var(--topbar) + 16px); -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  font-size: var(--fs-base);
  line-height: 1.7;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
ul, ol { list-style: none; }
a { color: inherit; }
img { display: block; max-width: 100%; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
h1, h2, h3, h4 { line-height: 1.25; font-weight: 600; }
time { font-variant-numeric: tabular-nums; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 6px; }

.icon { width: 1.1em; height: 1.1em; flex-shrink: 0; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.skip {
  position: absolute; left: 16px; top: -60px; z-index: 100;
  background: var(--accent); color: var(--accent-ink);
  padding: 8px 14px; border-radius: var(--radius-sm); text-decoration: none;
}
.skip:focus { top: 12px; }

/* ===== Top bar ===== */
.topbar {
  position: sticky; top: 0; z-index: 50;
  height: var(--topbar);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s;
}
.topbar.scrolled { border-bottom-color: var(--line); }
.topbar-inner {
  max-width: 1160px; height: 100%; margin: 0 auto; padding: 0 var(--gutter);
  display: flex; align-items: center; gap: 24px;
}
.mark {
  font-weight: 600; letter-spacing: 0.04em; text-decoration: none;
  width: 34px; height: 34px; display: grid; place-items: center;
  border: 1.5px solid var(--accent); color: var(--accent); border-radius: 9px; font-size: 0.85rem;
}
.nav { display: flex; gap: 4px; }
.nav a {
  text-decoration: none; color: var(--muted); font-size: var(--fs-sm);
  padding: 6px 12px; border-radius: 999px; transition: color 0.15s, background-color 0.15s;
}
.nav a:hover { color: var(--ink); }
.nav a[aria-current="true"] { color: var(--accent); background: var(--accent-soft); }
.tools { margin-left: auto; display: flex; gap: 6px; }
.tool {
  height: 36px; min-width: 36px; padding: 0 10px; border-radius: var(--radius-sm);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  color: var(--muted); font-size: var(--fs-sm); text-decoration: none;
  transition: color 0.15s, background-color 0.15s;
}
.tool:hover { color: var(--ink); background: var(--accent-soft); }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: block; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}

/* ===== Hero ===== */
.hero { position: relative; overflow: hidden; border-bottom: 1px solid var(--line); }
.traces {
  position: absolute; inset: 0 0 auto auto; width: min(1200px, 100%); height: 260px;
  pointer-events: none;
}
.trace-lines path {
  fill: none; stroke: var(--trace); stroke-width: 2; stroke-linejoin: round;
  stroke-dasharray: 1; stroke-dashoffset: 1;
  animation: draw 1.8s cubic-bezier(0.6, 0, 0.2, 1) forwards;
}
.trace-lines path:nth-child(2) { animation-delay: 0.08s; }
.trace-lines path:nth-child(3) { animation-delay: 0.16s; }
.trace-lines path:nth-child(4) { animation-delay: 0.24s; }
.trace-vias circle {
  fill: var(--bg); stroke: var(--copper); stroke-width: 2;
  opacity: 0; animation: via 0.4s ease-out 1.6s forwards;
}
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes via { to { opacity: 1; } }

.hero-inner {
  position: relative; max-width: 1160px; margin: 0 auto;
  padding: 72px var(--gutter) 40px;
  display: grid; grid-template-columns: auto 1fr; gap: 20px 32px; align-items: start;
}
.photo {
  width: 112px; height: 112px; border-radius: 28px; object-fit: cover;
  border: 1px solid var(--line); box-shadow: var(--shadow); background: var(--surface);
}
.hero h1 {
  font-size: var(--fs-name); letter-spacing: -0.02em; font-weight: 600;
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 18px;
}
.alt-name { font-size: 0.45em; font-weight: 400; color: var(--muted); letter-spacing: 0; }
.role-title { margin-top: 6px; font-size: var(--fs-md); color: var(--accent); font-weight: 500; }
.tagline { margin-top: 12px; max-width: 38em; font-size: var(--fs-md); color: var(--muted); }
.actions { margin-top: 24px; display: flex; flex-wrap: wrap; gap: 10px; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 42px; padding: 0 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--surface);
  text-decoration: none; font-size: var(--fs-sm); font-weight: 500;
  transition: border-color 0.15s, background-color 0.15s, color 0.15s;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { color: var(--accent-ink); filter: brightness(1.08); }
.btn-small { height: 34px; padding: 0 12px; }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.facts {
  grid-column: 1 / -1; margin-top: 16px;
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  border-top: 1px solid var(--line);
}
.fact { padding: 18px 20px 4px 0; }
.fact + .fact { padding-left: 20px; border-left: 1px solid var(--line); }
.fact dt { font-size: var(--fs-xl); font-weight: 600; color: var(--ink); line-height: 1.2; font-variant-numeric: tabular-nums; }
.fact dd { font-size: var(--fs-sm); color: var(--muted); margin-top: 2px; }

/* ===== Layout ===== */
.layout {
  max-width: 1160px; margin: 0 auto; padding: 40px var(--gutter) 64px;
  display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 48px; align-items: start;
}
.side {
  position: sticky; top: calc(var(--topbar) + 20px);
  display: flex; flex-direction: column; gap: 20px;
  max-height: calc(100vh - var(--topbar) - 40px); overflow-y: auto;
  scrollbar-width: thin; padding: 2px;
}
.content { display: flex; flex-direction: column; gap: 40px; min-width: 0; }

.block {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px;
}
.block-plain { background: none; border: 0; padding: 0; }
.block h2 { font-size: var(--fs-md); margin-bottom: 14px; }
.block-plain h2 { font-size: var(--fs-lg); }
#about p { max-width: 40em; color: var(--muted); }
#about p + p { margin-top: 10px; }

/* Skills */
.hint { font-size: var(--fs-sm); color: var(--muted); margin: -6px 0 14px; }
.skill-group + .skill-group { margin-top: 14px; }
.skill-group h3 { font-size: var(--fs-sm); font-weight: 500; color: var(--muted); margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-block; font-size: 0.8125rem; line-height: 1; padding: 7px 11px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--bg);
  transition: border-color 0.15s, background-color 0.15s, color 0.15s;
}
button.chip { text-decoration: underline; text-decoration-color: var(--trace); text-underline-offset: 3px; }
button.chip:hover { border-color: var(--accent); color: var(--accent); }
button.chip[aria-pressed="true"] {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink); text-decoration-color: transparent;
}
.chip-static { color: var(--muted); }

/* Education & contact */
.edu li + li { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--line); }
.edu h3 { font-size: var(--fs-base); }
.edu p { font-size: var(--fs-sm); color: var(--muted); }
.edu .period { font-size: 0.8125rem; }
.sub-head { font-size: var(--fs-sm); font-weight: 500; color: var(--muted); margin: 18px 0 8px; padding-top: 14px; border-top: 1px solid var(--line); }
.contact-list li { display: flex; align-items: center; gap: 10px; font-size: var(--fs-sm); padding: 5px 0; min-width: 0; }
.contact-list .icon { color: var(--accent); }
.contact-list a { text-decoration: none; overflow-wrap: anywhere; }
.contact-list a:hover { color: var(--accent); text-decoration: underline; }
.mini {
  margin-left: auto; width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center;
  color: var(--muted); flex-shrink: 0;
}
.mini:hover { background: var(--accent-soft); color: var(--accent); }

/* ===== Experience timeline ===== */
.exp-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.exp-head h2 { margin: 0; }
.filter-status {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  font-size: var(--fs-sm); padding: 10px 14px; margin-bottom: 16px;
  border-radius: var(--radius-sm); background: var(--accent-soft); color: var(--accent);
}
.filter-status[hidden] { display: none; }
.link-btn { text-decoration: underline; text-underline-offset: 3px; font-size: var(--fs-sm); color: var(--ink); }

.timeline { position: relative; padding-left: 32px; }
/* 時間軸主幹:像 PCB 走線 */
.timeline::before {
  content: ""; position: absolute; left: 7px; top: 14px; bottom: 14px; width: 2px;
  background: var(--trace); border-radius: 2px;
}
.job { position: relative; }
.job + .job { margin-top: 28px; }
.via {
  position: absolute; left: -32px; top: 6px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--bg); border: 2.5px solid var(--copper);
}
.job[data-current] .via { background: var(--copper); box-shadow: 0 0 0 5px color-mix(in srgb, var(--copper) 22%, transparent); }

.period { font-size: var(--fs-sm); color: var(--muted); display: flex; align-items: center; gap: 10px; }
.now {
  font-size: 0.75rem; font-weight: 500; color: var(--copper); padding: 1px 8px;
  border: 1px solid currentColor; border-radius: 999px; line-height: 1.6;
}
.job-head h3 { font-size: var(--fs-lg); margin-top: 2px; }
.company { color: var(--ink); font-weight: 500; margin-top: 2px; }
.onsite { display: block; font-weight: 400; color: var(--muted); font-size: var(--fs-sm); }
.summary { color: var(--muted); margin-top: 6px; max-width: 42em; }

.projects, .points {
  margin-top: 14px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden;
}
.project, .point {
  position: relative; padding: 16px 20px;
  transition: opacity 0.2s, background-color 0.2s;
}
.project + .project, .point + .point { border-top: 1px solid var(--line); }
.p-name { font-size: var(--fs-base); }
.p-toggle {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  text-align: left; font-weight: 600; line-height: 1.4;
}
.p-toggle:hover span { color: var(--accent); }
.chev { color: var(--muted); transition: transform 0.25s ease; }
.p-toggle[aria-expanded="true"] .chev { transform: rotate(180deg); color: var(--accent); }
.p-highlight { color: var(--muted); font-size: 0.9375rem; margin-top: 4px; max-width: 46em; }
.badge {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 8px;
  font-size: 0.8125rem; font-weight: 500; color: var(--copper);
}
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.tags li {
  font-size: 0.75rem; line-height: 1; padding: 5px 8px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent);
}
.point .tags { margin-top: 8px; }

.p-details { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.3s ease; }
.p-details.open { grid-template-rows: 1fr; }
.p-details-inner { overflow: hidden; }
.p-details ul { padding: 12px 0 2px; }
.p-details li {
  position: relative; padding-left: 18px; font-size: 0.9375rem; max-width: 46em;
}
.p-details li + li { margin-top: 6px; }
.p-details li::before {
  content: ""; position: absolute; left: 2px; top: 0.72em; width: 7px; height: 7px;
  border: 1.5px solid var(--copper); border-radius: 50%;
}

/* 技能篩選 */
.filtering .project, .filtering .point { opacity: var(--dim); }
.filtering .project.match, .filtering .point.match { opacity: 1; background: color-mix(in srgb, var(--accent-soft) 55%, transparent); }
.filtering .project.match::before, .filtering .point.match::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent);
}
.filtering .job:not(.has-match) .job-head { opacity: var(--dim); }
.filtering .tags li.hit { background: var(--accent); color: var(--accent-ink); }
.job-head { transition: opacity 0.2s; }

/* ===== Footer & toast ===== */
.footer {
  max-width: 1160px; margin: 0 auto; padding: 24px var(--gutter) 40px;
  border-top: 1px solid var(--line); font-size: var(--fs-sm); color: var(--muted);
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 24px;
}
.updated { margin-left: 12px; }
.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 60;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); font-size: var(--fs-sm);
  padding: 10px 16px; border-radius: 999px; box-shadow: var(--shadow);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ===== Tablet ===== */
@media (max-width: 1023px) {
  .layout { grid-template-columns: 1fr 1fr; gap: 20px; padding-top: 32px; }
  .side, .content { display: contents; }
  #about { grid-column: 1 / -1; order: 1; }
  #skills { grid-column: 1 / -1; order: 2; }
  #experience { grid-column: 1 / -1; order: 3; margin-top: 20px; }
  #education { order: 4; margin-top: 20px; }
  #contact { order: 5; margin-top: 20px; }
  #skills { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 28px; }
  #skills h2, #skills .hint { grid-column: 1 / -1; }
  #skills .skill-group + .skill-group { margin-top: 0; }
  #skills .skill-group { margin-bottom: 14px; }
  .traces { height: 64px; }
}

/* ===== Mobile ===== */
@media (max-width: 679px) {
  :root { --topbar: 52px; }
  .nav { display: none; }
  .tool-text span { display: none; }
  #lang-switch span { display: inline; }
  .hero-inner { grid-template-columns: 1fr; padding-top: 40px; gap: 16px; }
  .photo { width: 84px; height: 84px; border-radius: 22px; }
  .tagline, .role-title { font-size: var(--fs-base); }
  .actions .btn { flex: 1 1 auto; justify-content: center; }
  .actions a[href^="mailto"] { display: none; }
  .facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fact:nth-child(3) { padding-left: 0; border-left: 0; }
  .fact:nth-child(n + 3) { border-top: 1px solid var(--line); margin-top: 14px; }
  .layout { grid-template-columns: 1fr; }
  #education, #contact { grid-column: 1 / -1; margin-top: 0; }
  #education { margin-top: 20px; }
  #skills { grid-template-columns: 1fr; }
  .block { padding: 18px; }
  .timeline { padding-left: 26px; }
  .timeline::before { left: 5px; }
  .via { left: -26px; width: 14px; height: 14px; }
  .project, .point { padding: 14px 16px; }
  .exp-head { flex-wrap: wrap; }
  .traces { height: 40px; }
  .footer { flex-direction: column; }
  .updated { display: block; margin-left: 0; }
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-delay: 0s !important; }
}

/* ===== Print / PDF ===== */
@media print {
  @page { size: A4; margin: 10mm 11mm 11mm; }
  :root, :root[data-theme="dark"] {
    --bg: #fff; --surface: #fff; --ink: #1c1a3d; --muted: #4a4868; --line: #dcdae8;
    --accent: #4a3dc9; --accent-ink: #fff; --accent-soft: #efeefc; --trace: #c4bdf6; --copper: #a2602b;
    color-scheme: light;
  }
  html { font-size: 11px; }
  html[lang="en"] { font-size: 10px; }
  body { background: #fff; }
  .topbar, .skip, .traces, .actions, .exp-head .btn, .filter-status, .toast, .mini, .hint, .chev { display: none !important; }
  .hero { border: 0; }
  .hero-inner { padding: 0 0 8px; grid-template-columns: auto 1fr; }
  .photo { width: 72px; height: 72px; border-radius: 16px; box-shadow: none; }
  .facts { grid-column: 1 / -1; }
  .fact { padding-top: 8px; }
  .layout { display: grid; grid-template-columns: minmax(0, 1fr) 27%; gap: 22px; padding: 10px 0 0; }
  .side, .content { display: block; position: static; max-height: none; overflow: visible; }
  .content { grid-column: 1; grid-row: 1; }
  .side { grid-column: 2; grid-row: 1; padding: 0; }
  #about p, .summary, .p-highlight, .p-details li, .tagline { max-width: none; }
  #skills .skill-group { display: block; margin-bottom: 6px; }
  .skill-group h3 { display: block; margin: 0; }
  .contact-list { display: block; }
  .contact-list li { padding: 1px 0; }
  .block { border: 0; padding: 0; margin-bottom: 12px; }
  .side .block { break-inside: avoid; }
  .block h2, .block-plain h2 { font-size: 1.25rem; margin-bottom: 6px; }
  .content { gap: 12px; }
  .filtering .project, .filtering .point, .filtering .job-head { opacity: 1 !important; }
  .project::before, .point::before { display: none !important; }
  .tags li.hit { background: none !important; color: var(--accent) !important; }
  button.chip[aria-pressed="true"] { background: none; }
  .summary, .p-highlight { margin-top: 1px; }
  .p-details li + li { margin-top: 1px; }
  .p-details ul { padding-top: 2px !important; }
  .tags { margin-top: 3px; }
  .badge { margin-top: 2px; }
  body { line-height: 1.5; }
  #skills { display: block; }
  .chips { display: inline; }
  .chips li { display: inline; }
  .chip { border: 0; padding: 0 6px 0 0; background: none; text-decoration: none !important; color: var(--ink) !important; }
  .chips li:not(:last-child) .chip::after { content: ","; }
  .skill-group::after { content: ""; display: block; height: 4px; }
  .content { margin-top: 6px; }
  .timeline { padding-left: 0; }
  .timeline::before, .via { display: none; }
  .job + .job { margin-top: 14px; }
  .job-head { break-after: avoid; }
  .projects, .points { border: 0; margin-top: 4px; }
  .project, .point { padding: 6px 0; break-inside: avoid; opacity: 1 !important; background: none !important; }
  .project + .project, .point + .point { border-top: 0; }
  .p-details { grid-template-rows: 1fr !important; }
  .p-details ul { padding-top: 4px; }
  .tags li { background: none; padding: 0 6px 0 0; color: var(--accent); }
  .footer { border: 0; padding: 6px 0 0; }
  .footer p:first-child { display: none; }
  .p-details li { font-size: 0.875rem; }
  .job + .job { margin-top: 10px; }
  a { text-decoration: none !important; }
}
