/* ============================================================
   DevCalc Web — 全局样式
   设计令牌（Design Tokens）：竖屏/横屏共用，仅改变布局排列。
   ============================================================ */

:root {
  /* 品牌与语义色 */
  --primary: #2962FF;
  --on-primary: #FFFFFF;
  --primary-container: #E3ECFF;
  --on-primary-container: #0B1F47;
  --bg: #FAFAFA;
  --on-bg: #1A1A1C;
  --surface: #FFFFFF;
  --on-surface: #1A1A1C;
  --surface-variant: #F2F2F4;
  --body: #757575;
  --hint: #9E9E9E;
  --outline: #E0E0E0;
  --outline-strong: #C5C8CF;
  --error: #BA1A1A;
  --error-container: #FFDAD6;
  --on-error-container: #410002;
  --code-bg: #F5F5F5;
  --chip-bg: rgba(41, 98, 255, 0.10);
  --chip-text: #2962FF;

  /* 计算器键盘分层 */
  --key-num-bg: #FFFFFF;
  --key-num-text: #212121;
  --key-op-bg: #E8EDF7;
  --key-op-text: #37474F;
  --key-eq-bg: #2962FF;
  --key-eq-text: #FFFFFF;
  --key-danger-bg: #FFE0E0;
  --key-danger-text: #B71C1C;

  /* 尺寸 */
  --radius-card: 16px;
  --radius-code: 12px;
  --radius-input: 16px;
  --radius-chip: 999px;
  --shadow-card: 0 4px 12px rgba(0, 0, 0, 0.06);
  --shadow-sheet: 0 -8px 32px rgba(0, 0, 0, 0.12);
  --sp-4: 4px;
  --sp-8: 8px;
  --sp-12: 12px;
  --sp-16: 16px;
  --sp-20: 20px;
  --sp-24: 24px;
  --sp-32: 32px;
  --page-pad: 20px;

  /* 字体 */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC",
    "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;
}

[data-theme="dark"] {
  --primary: #8FB6FF;
  --on-primary: #00315E;
  --primary-container: #1B3D86;
  --on-primary-container: #D9E2FF;
  --bg: #0F0F11;
  --on-bg: #E4E4E6;
  --surface: #161618;
  --on-surface: #E4E4E6;
  --surface-variant: #1E1E20;
  --body: #B0B0B0;
  --hint: #757575;
  --outline: #36363A;
  --outline-strong: #55585E;
  --error: #FFB4AB;
  --error-container: #93000A;
  --on-error-container: #FFDAD6;
  --code-bg: #232326;
  --chip-bg: rgba(143, 182, 255, 0.14);
  --chip-text: #8FB6FF;
  --key-num-bg: #26262A;
  --key-num-text: #E8E8EA;
  --key-op-bg: #2E3440;
  --key-op-text: #C9D4E4;
  --key-eq-bg: #2962FF;
  --key-eq-text: #FFFFFF;
  --key-danger-bg: #3A2326;
  --key-danger-text: #FF9E9E;
  --shadow-card: 0 4px 12px rgba(0, 0, 0, 0.30);
  --shadow-sheet: 0 -8px 32px rgba(0, 0, 0, 0.45);
}

/* ---------- Reset / Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--on-bg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
#app, #portrait-view, #landscape-view { height: 100%; }
button {
  font-family: inherit;
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
  padding: 0;
}
input, textarea, select {
  font-family: inherit;
  color: inherit;
}
input[type="text"], input[type="number"], input[type="search"], textarea {
  font-family: var(--mono);
}

/* 滚动条 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--outline-strong); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ============================================================
   共享组件
   ============================================================ */

/* 工具栏 */
.toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 56px;
  padding: 0 var(--page-pad);
  background: var(--bg);
  border-bottom: 1px solid var(--outline);
  flex-shrink: 0;
  position: relative;
  z-index: 5;
}
.toolbar .tb-title {
  font-size: 20px;
  color: var(--body);
  letter-spacing: 0;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toolbar .tb-title strong { color: var(--on-bg); font-weight: 600; }
.tb-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--on-bg);
  transition: background .15s;
}
.tb-btn:hover { background: var(--surface-variant); }
.tb-btn svg { width: 22px; height: 22px; }

/* 卡片 */
.card {
  background: var(--surface);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: var(--sp-16);
}

/* 字段（label + 控件） */
.field { margin: 0 0 var(--sp-16); }
.field > label {
  display: block;
  font-size: 12.5px;
  color: var(--body);
  letter-spacing: .4px;
  margin-bottom: 6px;
}

/* 输入框 */
.input-wrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--outline-strong);
  border-radius: var(--radius-input);
  transition: border-color .15s, box-shadow .15s;
}
.input-wrap:focus-within {
  border-color: var(--primary);
  border-width: 2px;
  box-shadow: 0 0 0 3px var(--chip-bg);
  margin: -1px;
}
.input-wrap.error { border-color: var(--error); }
.input-wrap textarea, .input-wrap input {
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  padding: 11px var(--sp-16);
  font-size: 22px;
  border-radius: var(--radius-input);
  resize: vertical;
  min-height: 48px;
}
.input-wrap textarea { font-size: 15px; line-height: 1.5; min-height: 96px; }
.input-wrap input::placeholder, .input-wrap textarea::placeholder { color: var(--hint); }

/* 输出卡 */
.output {
  background: var(--code-bg);
  border-radius: var(--radius-card);
  padding: 10px 14px;
  min-height: 48px;
  font-family: var(--mono);
  font-size: 16px;
  color: var(--on-surface);
  word-break: break-all;
  white-space: pre-wrap;
  position: relative;
  overflow: hidden;
  user-select: all;
}
.output.out-big { font-size: 22px; font-weight: 700; word-break: break-all; }
.output.out-huge { font-size: 34px; font-weight: 700; }
.output.selectable { user-select: text; cursor: text; }

/* 复制按钮（圆形小图标） */
.copy {
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--body);
  background: color-mix(in srgb, var(--surface) 70%, transparent);
}
.copy:hover { background: var(--surface-variant); color: var(--primary); }
.copy svg { width: 18px; height: 18px; }

/* chip 单选组 */
.chips { display: flex; flex-wrap: wrap; gap: var(--sp-8); }
.chip {
  padding: 7px 16px;
  border-radius: var(--radius-chip);
  border: 1px solid var(--outline-strong);
  background: var(--surface);
  color: var(--on-surface);
  font-size: 14px;
  transition: background .12s, color .12s, border-color .12s;
}
.chip:hover { border-color: var(--primary); }
.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}

/* 分段单选（DEG/RAD、模式） */
.seg {
  display: flex;
  border: 1px solid var(--outline-strong);
  border-radius: var(--radius-input);
  overflow: hidden;
  background: var(--surface);
}
.seg button {
  flex: 1;
  padding: 10px 0;
  font-size: 14px;
  color: var(--on-surface);
  background: transparent;
  transition: background .12s, color .12s;
}
.seg button + button { border-left: 1px solid var(--outline-strong); }
.seg button.active { background: var(--primary-container); color: var(--primary); font-weight: 600; }

/* 主按钮 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 24px;
  border-radius: var(--radius-input);
  background: var(--primary);
  color: var(--on-primary);
  font-size: 15px;
  font-weight: 600;
  transition: transform .08s, opacity .15s;
  user-select: none;
}
.btn:active { transform: scale(.96); }
.btn.btn-ghost { background: var(--surface); color: var(--primary); border: 1px solid var(--primary); }
.btn.btn-sm { padding: 6px 16px; font-size: 13px; min-height: 34px; }
.btn:disabled { opacity: .5; }

/* 关键值行（label/value） */
.lvr { display: flex; align-items: baseline; gap: 12px; padding: 6px 0; }
.lvr .k { color: var(--body); font-size: 13px; width: 90px; flex-shrink: 0; }
.lvr .v { font-family: var(--mono); font-size: 14px; text-align: right; flex: 1; word-break: break-all; }

/* 哈希行 */
.hrow { display: flex; align-items: center; gap: 8px; padding: 8px 0; }
.hrow .k { color: var(--body); font-size: 13px; width: 100px; flex-shrink: 0; text-transform: uppercase; }
.hrow .v { font-family: var(--mono); font-size: 13px; flex: 1; word-break: break-all; background: var(--code-bg); border-radius: 8px; padding: 6px 10px; }
.hrow .copy-sm {
  width: 30px; height: 30px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--body); flex-shrink: 0;
}
.hrow .copy-sm:hover { color: var(--primary); background: var(--surface-variant); }
.hrow .copy-sm svg { width: 16px; height: 16px; }

/* 高亮 mark（正则） */
mark { background: #FFF176; color: #111; border-radius: 2px; }
[data-theme="dark"] mark { background: #6B6200; color: #FFF; }

/* 代码块（速查表） */
.code {
  font-family: var(--mono);
  font-size: 15px;
  background: var(--code-bg);
  border-radius: var(--radius-code);
  padding: 10px 12px;
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 1.5;
}

/* ============================================================
   竖屏 Portrait —— home 宫格 + 整页工具
   ============================================================ */

#portrait-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}
#p-page { flex: 1; overflow-y: auto; padding: var(--sp-12) var(--page-pad) calc(24px + env(safe-area-inset-bottom, 0px)); }

/* 首页宫格 */
.p-home-title {
  font-size: 18px;
  font-weight: 700;
  margin: 6px 0 4px;
  padding: 0 4px;
}
.p-home-sub { font-size: 13px; color: var(--body); margin: 0 0 12px; padding: 0 4px; }
.tool-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--sp-8);
}
.tool-card {
  background: var(--surface);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px;
  min-height: 96px;
  aspect-ratio: 1.05;
  max-height: 140px;
  cursor: pointer;
  text-align: center;
  transition: transform .1s, box-shadow .1s;
  overflow: hidden;
}
.tool-card:hover { box-shadow: 0 8px 22px rgba(0,0,0,.10); }
.tool-card:active { transform: scale(.97); }
.tool-card .tc-icon { width: 32px; height: 32px; color: var(--primary); }
.tool-card .tc-icon svg { width: 32px; height: 32px; }
.tool-card .tc-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--on-surface);
  line-height: 1.2;
  width: 100%;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.tool-card .tc-cat {
  font-size: 10.5px;
  letter-spacing: .3px;
  color: var(--body);
  white-space: nowrap;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 工具页内容（竖屏单栏堆叠；横屏由下方媒体查询转为左右双栏） */
.tool-page {
  padding: var(--sp-12) 0 0;
  animation: pageIn .3s cubic-bezier(.2, .8, .25, 1);
}
/* 双栏根容器：竖屏纵向堆叠，横屏媒体查询转左右并排 */
.tool-pane {
  display: flex;
  flex-direction: column;
  width: 100%;
}
.tp-left, .tp-right {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
/* 竖屏：科学计算器结果区置顶（复刻安卓：结果显示在键盘上方） */
.tp-order-swap { order: -1; }
@keyframes pageIn {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* 科学计算器键盘 */
.calc-panel .expr-output { margin-bottom: 12px; }
.keys {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  margin-top: 10px;
}
.keys button {
  min-height: 54px;
  border-radius: var(--radius-input);
  font-size: 22px;
  font-weight: 600;
  color: var(--key-num-text);
  background: var(--key-num-bg);
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
  transition: transform .06s, filter .1s;
  user-select: none;
}
.keys button:active { transform: scale(.93); }
.keys .k-op { background: var(--key-op-bg); color: var(--key-op-text); font-size: 13px; }
.keys .k-eq { background: var(--key-eq-bg); color: var(--key-eq-text); }
.keys .k-danger { background: var(--key-danger-bg); color: var(--key-danger-text); }

/* ============================================================
   横屏 Landscape —— Sidebar + TabBar + 内容区
   ============================================================ */

#landscape-view {
  display: flex;
  height: 100%;
  background: var(--bg);
}
.land-sidebar {
  width: 214px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--outline);
  overflow-y: auto;
  padding: 10px 8px 20px;
}
.land-sidebar .ls-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 12px;
  font-size: 16px;
  font-weight: 700;
}
.land-sidebar .ls-brand svg { width: 22px; height: 22px; color: var(--primary); }
.ls-cat {
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--hint);
  padding: 10px 12px 4px;
}
.ls-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 14px;
  color: var(--on-surface);
  text-align: left;
  transition: background .12s;
}
.ls-item svg { width: 18px; height: 18px; color: var(--body); flex-shrink: 0; }
.ls-item:hover { background: var(--surface-variant); }
.ls-item.on { background: var(--primary-container); color: var(--primary); font-weight: 600; }
.ls-item.on svg { color: var(--primary); }

.land-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.land-tabbar {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border-bottom: 1px solid var(--outline);
  padding: 0 6px;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}
.land-tabbar::-webkit-scrollbar { display: none; }
.land-tab {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 12px;
  font-size: 13px;
  color: var(--body);
  white-space: nowrap;
  border-bottom: 3px solid transparent;
  transition: color .12s;
  cursor: pointer;
  flex-shrink: 0;
}
.land-tab svg { width: 15px; height: 15px; }
.land-tab:hover { color: var(--on-surface); }
.land-tab.active {
  color: var(--on-surface);
  border-bottom-color: var(--primary);
}
.land-tab .tab-x {
  width: 18px; height: 18px;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; line-height: 1;
  color: var(--hint);
}
.land-tab .tab-x:hover { background: var(--surface-variant); color: var(--on-surface); }
.land-content { flex: 1; overflow-y: auto; padding: 14px 20px 24px; min-width: 0; }

/* ============================================================
   浮层：Bottom Sheet / Modal / Snackbar
   ============================================================ */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn .18s ease;
}
.overlay.modal-center { align-items: center; padding: 24px; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 560px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 10px var(--sp-24) calc(20px + env(safe-area-inset-bottom, 0px));
  box-shadow: var(--shadow-sheet);
  animation: sheetUp .22s cubic-bezier(.2, .8, .25, 1);
}
@keyframes sheetUp { from { transform: translateY(40px); } to { transform: translateY(0); } }
.sheet .sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 0 10px;
  font-size: 16px; font-weight: 600;
}
.sheet .sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--outline-strong); margin: 6px auto 14px; }

.modal {
  background: var(--surface);
  border-radius: 20px;
  padding: 22px;
  width: 100%;
  max-width: 420px;
  max-height: 82vh;
  overflow-y: auto;
  box-shadow: var(--shadow-sheet);
  animation: fadeIn .16s ease;
}
.modal h3 { margin: 0 0 10px; font-size: 18px; }
.modal p { color: var(--body); margin: 0 0 8px; }

.hlist { list-style: none; margin: 0; padding: 0; }
.hlist li {
  padding: 12px 4px;
  border-bottom: 1px solid var(--outline);
  font-family: var(--mono);
  font-size: 14px;
  word-break: break-all;
  cursor: pointer;
}
.hlist li:hover { color: var(--primary); }
.hlist .hlist-empty {
  text-align: center; color: var(--hint);
  font-family: var(--font); padding: 28px 0; cursor: default;
}

.snackbar {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 11px 18px;
  border-radius: 10px;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  z-index: 90;
  animation: snackIn .2s ease;
  pointer-events: none;
}
@keyframes snackIn { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* 颜色工具 */
.color-preview {
  width: 100%;
  height: 88px;
  border-radius: var(--radius-card);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-weight: 700;
  cursor: pointer;
  transition: transform .08s;
  background: #888;
}
.color-preview:active { transform: scale(.98); }
.color-swatches { display: flex; gap: 8px; }
[data-theme="dark"] input[type="range"] { accent-color: var(--primary); }
.range-row { margin: 14px 0 0; }
.range-row .rr-top { display: flex; justify-content: space-between; font-size: 13px; color: var(--body); margin-bottom: 4px; }
.range-row input[type="range"] { width: 100%; margin: 0; }

/* 速查表 */
.cheat-section h2 {
  font-size: 15px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--primary);
  margin: 20px 0 10px;
}
.cheat-entry {
  background: var(--surface);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 12px 14px;
  margin-bottom: 10px;
  position: relative;
}
.cheat-tag {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  background: var(--chip-bg);
  color: var(--chip-text);
  padding: 2px 8px;
  border-radius: var(--radius-chip);
  margin-bottom: 8px;
  letter-spacing: .5px;
}
.cheat-entry .code { margin-bottom: 8px; padding-right: 40px; }
.cheat-desc { font-size: 13.5px; color: var(--body); margin: 0; }

/* 页面切换过渡锚点 */
.fade-body { transition: background .2s, color .2s; }

/* ============================================================
   方向切换（JS 在 <body> 上设置 .is-portrait / .is-landscape）
   ============================================================ */
html[data-orientation="portrait"] #landscape-view { display: none; }
html[data-orientation="landscape"] #portrait-view { display: none; }

/* 窄屏（<500px 且横屏，如手机横放）时侧栏收窄 */
@media (max-width: 700px) and (orientation: landscape) {
  .land-sidebar { width: 150px; }
}

/* ============================================================
   横屏（桌面/平板）：工具内容区改为左右双栏工作台
   左侧 = 输入/操作区，右侧 = 输出/结果区，避免移动端拉长观感
   ============================================================ */
@media (orientation: landscape) {
  #landscape-view .tool-pane {
    flex-direction: row;
    align-items: stretch;
  }
  #landscape-view .tp-left {
    flex: 0 0 40%;
    padding-right: 16px;
  }
  #landscape-view .tp-right {
    flex: 1 1 0;
    padding-left: 16px;
    border-left: 1px solid var(--outline);
  }
  /* 右栏各输出块均匀拉伸填充高度，避免右侧大面积空白 */
  #landscape-view .tp-right > .field,
  #landscape-view .tp-right > .card,
  #landscape-view .tp-right > div:not(.field):not(.card):not(.tp-order-swap) {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }
  #landscape-view .tp-right .output {
    flex: 1 1 auto;
    min-height: 64px;
  }
  #landscape-view .tp-order-swap { order: 0; }
  /* 速查表横屏两列排布 */
  #landscape-view .cheatsheet .cheat-section {
    columns: 2;
    column-gap: 16px;
  }
  #landscape-view .cheatsheet .cheat-entry {
    break-inside: avoid;
  }
}
