/* 函数画板
   VOICE: 安静、可靠的教学工具；图是主角
   IDEA: 坐标纸 + 老师的批注栏
   PICTURE: 画布占据主区域，滑块紧挨着题目信息
   COLOUR and TYPE: 纸白、墨蓝、朱红强调；系统中文字体
   LAYOUT: 桌面左栏右图，窄屏图在上、面板在下 */
:root {
  --bg: #f4f6f9; --paper: #fff; --ink: #16233a; --mute: #5d6b82; --line: #dfe3ea; --soft: #eef1f6;
  --acc: #e4572e; --acc-soft: #fdece7; --ok: #1b998b; --bad: #c0392b;
  --font: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  --side: 400px;
}
* { box-sizing: border-box; min-width: 0; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.6 var(--font); display: flex; flex-direction: column; overflow: hidden; }
button, input, textarea { font: inherit; color: inherit; }
button { height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); cursor: pointer; white-space: nowrap; }
button:hover { border-color: var(--ink); }
button:disabled { opacity: .55; cursor: default; border-color: var(--line); }
button.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
button.primary:hover:not(:disabled) { background: #24375a; }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--soft); }
button.wide { width: 100%; height: 44px; font-weight: 600; margin-top: 14px; }
:focus-visible { outline: 2px solid var(--acc); outline-offset: 2px; }
input[type=text], input[type=password], input:not([type]), textarea {
  width: 100%; font-size: 16px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); resize: vertical;
}
textarea { line-height: 1.55; }
.muted { color: var(--mute); }
.small { font-size: 13px; }
.lbl { display: block; font-size: 13px; font-weight: 600; margin: 14px 0 6px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.row.end { justify-content: flex-end; }
.row input { flex: 1 1 200px; }

/* 顶栏 */
.top { flex: none; height: 54px; display: flex; align-items: center; gap: 12px; padding: 0 14px 0 18px; background: var(--paper); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; color: var(--ink); text-decoration: none; margin-right: auto; }
.topnav { display: flex; gap: 4px; }

/* 主体 */
.app { flex: 1; min-height: 0; display: grid; grid-template-columns: var(--side) minmax(0, 1fr); }
.side { order: -1; background: var(--paper); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.stage { position: relative; min-height: 0; }
.board { position: absolute; inset: 0; background: #fff; touch-action: none; }
.fb-canvas { display: block; cursor: crosshair; touch-action: none; }
.tools { position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; gap: 6px; }
.tools button { width: 40px; height: 40px; padding: 0; display: grid; place-items: center; background: rgba(255, 255, 255, .94); box-shadow: 0 1px 3px rgba(22, 35, 58, .12); }
.tools svg { width: 20px; height: 20px; fill: none; stroke: var(--ink); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.hint { position: absolute; left: 12px; bottom: 8px; margin: 0; font-size: 12px; color: var(--mute); background: rgba(255, 255, 255, .85); padding: 2px 8px; border-radius: 6px; pointer-events: none; }

/* 侧栏 */
.tabs { flex: none; display: flex; padding: 10px 16px 0; gap: 4px; border-bottom: 1px solid var(--line); }
.tabs button { border: 0; border-radius: 8px 8px 0 0; background: transparent; height: 40px; color: var(--mute); font-weight: 600; border-bottom: 2px solid transparent; }
.tabs button[aria-selected=true] { color: var(--ink); border-bottom-color: var(--acc); }
.pane { flex: 1; overflow-y: auto; padding: 4px 18px 28px; }
.titlebar { padding: 14px 0 6px; }
.titlebar h1 { font-size: 21px; line-height: 1.3; margin: 0; text-wrap: balance; }
.titlebar p { margin: 4px 0 0; }
.block { border-top: 1px solid var(--line); padding: 4px 0 8px; }
.block summary { cursor: pointer; font-weight: 600; font-size: 14px; padding: 8px 0; list-style-position: inside; }
.pre { white-space: pre-wrap; margin: 2px 0 8px; }
.tips { margin: 6px 0 4px; padding-left: 1.2em; }
.tips li { margin: 3px 0; }
.warn { background: #fff7e6; border: 1px solid #f3d59b; border-radius: 8px; padding: 8px 10px; font-size: 13px; margin: 6px 0; }
.warn ul { margin: 4px 0 0; padding-left: 1.2em; }

/* 参数 */
.params { background: var(--soft); border-radius: 10px; padding: 6px 12px 10px; margin: 8px 0 12px; }
.params[hidden] { display: none; }
.param { padding: 6px 0 2px; }
.param + .param { border-top: 1px solid #e2e6ee; }
.phead { display: flex; align-items: center; gap: 8px; }
.phead label { flex: 1; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pval { width: 84px !important; flex: none; height: 32px; padding: 0 8px !important; text-align: right; font-variant-numeric: tabular-nums; font-size: 15px !important; }
.play { width: 32px; height: 32px; padding: 0; display: grid; place-items: center; flex: none; }
.param.anim .play { border-color: var(--acc); color: var(--acc); }
.param.playing .play { background: var(--acc); border-color: var(--acc); color: #fff; }
.prange { width: 100%; accent-color: var(--acc); height: 28px; margin: 2px 0 0; }

/* 对象列表 */
.objs { list-style: none; margin: 0; padding: 0; }
.objs li { display: flex; align-items: flex-start; gap: 8px; padding: 5px 0; font-size: 14px; }
.objs input { margin-top: 4px; width: 16px; height: 16px; accent-color: var(--ink); flex: none; }
.objs .sw { width: 14px; height: 4px; border-radius: 2px; margin-top: 10px; flex: none; }
.objs .sw.dash { background-image: linear-gradient(90deg, transparent 50%, var(--paper) 50%) !important; background-size: 6px 4px; }
.objs label { overflow-wrap: anywhere; cursor: pointer; }

#json { font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre; overflow: auto; }

/* 上传 */
.drop { display: grid; place-items: center; min-height: 84px; border: 1.5px dashed #c5ccd8; border-radius: 10px; color: var(--mute); text-align: center; padding: 12px; cursor: pointer; background: #fafbfc; }
.drop.over { border-color: var(--acc); background: var(--acc-soft); color: var(--ink); }
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.thumb { position: relative; width: 84px; height: 84px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: #fff; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb button { position: absolute; top: 3px; right: 3px; width: 26px; height: 26px; padding: 0; border-radius: 13px; background: rgba(22, 35, 58, .75); color: #fff; border: 0; font-size: 15px; line-height: 1; }
.status { font-size: 14px; margin-top: 10px; }
.status.err { color: var(--bad); }
.status.ok { color: var(--ok); }
.status ul { margin: 4px 0 0; padding-left: 1.2em; font-size: 13px; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--acc); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* 历史抽屉 */
.drawer { position: fixed; inset: 0; background: rgba(22, 35, 58, .35); z-index: 20; }
.drawer[hidden] { display: none; }
.drawer-panel { position: absolute; top: 0; bottom: 0; left: 0; width: min(380px, 92vw); background: var(--paper); display: flex; flex-direction: column; box-shadow: 8px 0 24px rgba(0, 0, 0, .15); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 17px; }
.hist { list-style: none; margin: 0; padding: 6px 0; overflow-y: auto; flex: 1; }
.hist li { display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 18px; }
.hist li:hover { background: var(--soft); }
.hist a { flex: 1; color: var(--ink); text-decoration: none; min-height: 44px; display: flex; flex-direction: column; justify-content: center; }
.hist a small { color: var(--mute); }
.hist li.cur a { font-weight: 700; }
#histEmpty { padding: 18px; }

/* 设置 */
.fs { border: 0; border-top: 1px solid var(--line); margin: 14px 0 0; padding: 10px 0 0; min-width: 0; }
.fs legend { font-weight: 700; padding: 0 6px 0 0; }
.radio, .check { display: flex; align-items: flex-start; gap: 8px; margin: 8px 0; cursor: pointer; }
.radio input, .check input { margin-top: 5px; width: 16px; height: 16px; accent-color: var(--ink); flex: none; }
.own { margin: 4px 0 0 24px; padding-left: 12px; border-left: 2px solid var(--line); }
.own[hidden] { display: none; }
select { width: 100%; height: 40px; font: inherit; font-size: 16px; padding: 0 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink); }
input[type=url] { width: 100%; font-size: 16px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); }
.row.tight { margin-top: 0; align-items: center; }
.status.inline { margin: 0; }
dialog { max-height: 92vh; }

/* 对话框 */
dialog { border: 0; border-radius: 12px; padding: 0; width: min(520px, 94vw); box-shadow: 0 20px 60px rgba(0, 0, 0, .25); color: var(--ink); }
dialog::backdrop { background: rgba(22, 35, 58, .4); }
.dlg { padding: 18px 20px; }
.dlg h2 { margin: 0 0 4px; font-size: 18px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 16px); opacity: 0; background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 8px; font-size: 14px; pointer-events: none; transition: .2s; z-index: 50; max-width: 90vw; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }

/* 窄屏：图在上，面板在下，整页滚动 */
@media (max-width: 860px) {
  body { overflow: auto; height: auto; }
  .app { display: block; }
  .stage { height: 58vh; min-height: 320px; border-bottom: 1px solid var(--line); }
  .side { border-right: 0; }
  .pane { overflow: visible; }
  .hint { display: none; }
  .tools { top: 8px; right: 8px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ===== 通用补充 ===== */
[hidden] { display: none !important; }
.btnlink { display: inline-flex; align-items: center; height: 38px; padding: 0 14px; border-radius: 8px; text-decoration: none; color: var(--ink); white-space: nowrap; }
.btnlink.ghost:hover { background: var(--soft); }
.btnlink.primary { background: var(--ink); color: #fff; }
.who { font-size: 14px; color: var(--mute); padding: 0 6px; max-width: 10em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.notice { background: var(--soft); border-radius: 10px; padding: 12px 14px; margin: 14px 0; }
.notice a { color: var(--acc); font-weight: 600; }
button.sm { height: 34px; padding: 0 12px; }
button.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.warn-btn { border-color: var(--bad) !important; color: var(--bad); }

/* 设置对话框 */
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.subtabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 6px 0 4px; }
.subtabs button { border: 0; background: transparent; border-radius: 8px 8px 0 0; color: var(--mute); font-weight: 600; border-bottom: 2px solid transparent; }
.subtabs button[aria-selected=true] { color: var(--ink); border-bottom-color: var(--acc); }
.sub { padding-top: 8px; }
.sub input[type=password], .sub input[type=text], .sub input:not([type]) { width: 100%; }

/* 历史抽屉 */
.drawer-tools { display: flex; gap: 8px; padding: 10px 12px 4px 18px; }
.drawer-tools input { flex: 1; font-size: 16px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; }
.drawer-tools select { width: auto; flex: none; }
#histMore { margin: 4px 18px 16px; }

/* ===== 画笔工具条 ===== */
.penbar { position: absolute; top: 12px; left: 12px; right: 64px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; padding: 6px 8px;
  background: rgba(255, 255, 255, .97); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 2px 10px rgba(22, 35, 58, .1); z-index: 3; width: max-content; max-width: calc(100% - 76px); }
.penbar .seg { display: flex; gap: 2px; padding: 2px; background: var(--soft); border-radius: 9px; }
.penbar .seg button { width: 36px; height: 36px; padding: 0; display: grid; place-items: center; border: 0; background: transparent; border-radius: 7px; }
.penbar .seg button[aria-pressed=true] { background: var(--ink); }
.penbar .seg button[aria-pressed=true] svg { stroke: #fff; }
.penbar .seg button:disabled { opacity: .35; }
.penbar svg { width: 20px; height: 20px; fill: none; stroke: var(--ink); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.colors { display: flex; gap: 4px; }
.sw-btn { width: 26px; height: 26px; padding: 0; border-radius: 50%; background: var(--c); border: 2px solid #fff; box-shadow: 0 0 0 1px #c9d0db; }
.sw-btn[aria-pressed=true] { box-shadow: 0 0 0 2px var(--ink); }
.custom { width: 28px; height: 28px; border-radius: 50%; overflow: hidden; box-shadow: 0 0 0 1px #c9d0db; flex: none; background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); cursor: pointer; }
.custom input { opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.wsl, .osl { display: flex; align-items: center; gap: 6px; }
.wsl input, .osl input { width: 90px; accent-color: var(--ink); }
.wdot { width: 26px; height: 26px; display: grid; place-items: center; }
.wdot i { display: block; border-radius: 50%; }
.wsl output { width: 2ch; font-size: 13px; color: var(--mute); font-variant-numeric: tabular-nums; }
.mini { font-size: 12px; color: var(--mute); }
#inkSave.dirty { background: var(--acc); border-color: var(--acc); }
.tools button[aria-pressed=true] { background: var(--ink); }
.tools button[aria-pressed=true] svg { stroke: #fff; }

/* ===== 管理页 ===== */
body.page { overflow: auto; height: auto; min-height: 100%; }
.admin { max-width: 1240px; width: 100%; margin: 0 auto; padding: clamp(18px, 3vw, 36px) max(4vw, 16px) 48px; }
.admin-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.admin-head h1 { margin: 0; font-size: 28px; }
.admin-head p { margin: 4px 0 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin: 18px 0; }
.stats div { background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.stats b { display: block; font-size: 26px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stats span { color: var(--mute); font-size: 13px; }
.filter input { max-width: 320px; }
.tablewrap { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; margin-top: 12px; overflow-x: auto; }
table.users { width: 100%; border-collapse: collapse; font-size: 14px; }
table.users th { text-align: left; font-size: 13px; color: var(--mute); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.users td { padding: 10px 12px; border-bottom: 1px solid var(--soft); vertical-align: top; }
table.users tr:last-child td { border-bottom: 0; }
table.users .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.users tr.off td { color: var(--mute); }
table.users .acts { white-space: nowrap; text-align: right; }
table.users .acts button { height: 32px; padding: 0 10px; margin-left: 4px; }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--acc-soft); color: var(--acc); font-weight: 600; }
.tag.off { background: var(--soft); color: var(--mute); }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 14px; }
.check-cell { display: flex; align-items: flex-end; padding-bottom: 6px; }
dialog { width: min(600px, 94vw); }
@media (max-width: 600px) {
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  .penbar { right: 56px; max-width: calc(100% - 64px); gap: 6px; }
  .wsl input, .osl input { width: 64px; }
  .osl .mini { display: none; }
  table.users { font-size: 13px; }
  .stack thead { display: none; }
  .stack tr { display: grid; padding: 10px 12px; border-bottom: 1px solid var(--line); }
  .stack td { display: flow-root; border: 0 !important; padding: 3px 0 !important; padding-inline-start: 7em !important; text-align: start !important; }
  .stack td::before { content: attr(data-l); float: inline-start; width: 6.5em; margin-inline-start: -7em; color: var(--mute); }
  .stack td.acts { padding-inline-start: 0 !important; }
  .stack td:empty { display: none; }
}

/* 修正：用户名对齐、窄屏画笔条单行横向滚动 */
.who { display: inline-flex; align-items: center; height: 38px; }
@media (max-width: 600px) {
  .who { display: none; }
  .topnav { gap: 0; }
  .topnav .ghost, .topnav .btnlink { padding: 0 9px; }
  .penbar { flex-wrap: nowrap; overflow-x: auto; overscroll-behavior: contain; scrollbar-width: none; width: auto; left: 8px; top: 8px; padding: 4px 6px; }
  .penbar::-webkit-scrollbar { display: none; }
  .penbar > * { flex: none; }
  .penbar .seg button { width: 34px; height: 34px; }
  .colors { gap: 3px; }
  .sw-btn { width: 24px; height: 24px; }
}
