:root {
  --bg: #eef2f8;
  --bg-2: #e3eaf6;
  --panel: #ffffff;
  --ink: #16202e;
  --muted: #71809a;
  --line: #e4eaf3;
  --brand: #2f6fed;
  --brand-deep: #2059cc;
  --brand-soft: #e9f0ff;
  --danger: #e2574c;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  position: fixed;
  inset: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", Arial, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(120% 90% at 15% 0%, #ffffff 0%, rgba(255, 255, 255, 0) 55%),
    linear-gradient(160deg, #f4f7fc 0%, var(--bg) 45%, var(--bg-2) 100%);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: 0;
  background: none;
  color: inherit;
}

/* ============ 屏幕切换 ============ */
.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  width: 100%;
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
}
.screen.is-active { display: flex; }

/* ============ 首页 ============ */
.home {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 40px;
  padding: 32px 24px calc(32px + var(--safe-b));
  max-width: 520px;
  margin: 0 auto;
  width: 100%;
}

.brand { text-align: center; }

.mark {
  width: 76px;
  height: 76px;
  margin: 0 auto 22px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  font-size: 40px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(150deg, #4c8bff 0%, var(--brand) 55%, var(--brand-deep) 100%);
  box-shadow: 0 14px 30px -10px rgba(47, 111, 237, 0.65), inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.brand h1 {
  margin: 0 0 10px;
  font-size: 27px;
  letter-spacing: 0.5px;
  font-weight: 650;
}

.brand p {
  margin: 0;
  font-size: 15px;
  line-height: 1.6;
  color: var(--muted);
}

.home-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 3px;
  min-height: 66px;
  padding: 14px 20px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 6px 18px -12px rgba(22, 32, 46, 0.45);
  transition: transform 0.12s ease, box-shadow 0.18s ease, background 0.18s ease;
  text-align: left;
}

.btn:active { transform: scale(0.985); }

.btn-title { font-size: 17px; font-weight: 600; }
.btn-sub { font-size: 13px; color: var(--muted); }

.btn--primary {
  background: linear-gradient(160deg, #4c8bff 0%, var(--brand) 60%, var(--brand-deep) 100%);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 16px 28px -14px rgba(47, 111, 237, 0.85);
}
.btn--primary .btn-sub { color: rgba(255, 255, 255, 0.82); }

.btn--wide { width: 100%; align-items: center; }

.btn:disabled {
  opacity: 0.45;
  box-shadow: none;
}

.home-foot {
  margin: 0;
  font-size: 12.5px;
  color: var(--muted);
  letter-spacing: 0.4px;
  text-align: center;
}

/* ============ 输入房间号 ============ */
.join {
  flex: 1;
  max-width: 480px;
  width: 100%;
  margin: 0 auto;
  padding: calc(20px + var(--safe-t)) 24px calc(28px + var(--safe-b));
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 14px;
}

.backbtn {
  position: absolute;
  top: calc(14px + var(--safe-t));
  left: 18px;
  font-size: 15px;
  color: var(--muted);
  padding: 8px 10px;
}

.join h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 650;
}

.join-hint {
  margin: 0 0 14px;
  font-size: 14px;
  color: var(--muted);
}

.code-box { position: relative; }

.code-cells {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
}

.code-cells span {
  height: 72px;
  border-radius: 14px;
  background: var(--panel);
  border: 1.5px solid var(--line);
  display: grid;
  place-items: center;
  font-size: 34px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.code-box.is-focus .code-cells span { border-color: #c9dbfb; }

.code-cells span.is-filled {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.12);
}

.code-field {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  font-size: 16px;
  color: transparent;
  background: transparent;
  caret-color: transparent;
  letter-spacing: 40px;
  padding-left: 20px;
}

.join-error {
  min-height: 20px;
  margin: 0;
  font-size: 13.5px;
  color: var(--danger);
}

/* ============ 教室 ============ */
.topbar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  padding: calc(9px + var(--safe-t)) 12px 9px;
  min-height: 54px;
  flex: 0 0 auto;
}

.iconbtn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 26px;
  line-height: 1;
  color: var(--muted);
  display: grid;
  place-items: center;
  padding-bottom: 4px;
}
.iconbtn:active { background: rgba(22, 32, 46, 0.06); }

.roomtag {
  display: flex;
  flex: 0 0 auto;
  white-space: nowrap;
  align-items: baseline;
  gap: 8px;
  padding: 7px 14px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
}

.roomtag-label { font-size: 12px; color: var(--muted); }

.roomtag b {
  font-size: 19px;
  font-weight: 650;
  letter-spacing: 2px;
  font-variant-numeric: tabular-nums;
  color: var(--brand-deep);
}

.conn {
  margin-left: auto;
  flex: 0 0 auto;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  padding-right: 4px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #c8d2e2;
  transition: background 0.2s ease;
}
.dot.is-on { background: #2ec27e; box-shadow: 0 0 0 4px rgba(46, 194, 126, 0.16); }
.dot.is-off { background: var(--danger); box-shadow: 0 0 0 4px rgba(226, 87, 76, 0.14); }

.board {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
}

.frame {
  position: relative;
  background: #fff;
  border-radius: 10px;
  box-shadow:
    0 22px 45px -24px rgba(22, 32, 46, 0.42),
    0 2px 6px rgba(22, 32, 46, 0.05);
  overflow: hidden;
  isolation: isolate;
}

.frame canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

#inkLayer {
  touch-action: none;
  cursor: crosshair;
  z-index: 2;
}

#gridLayer { z-index: 1; }

.board-tip {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(6px);
  padding: 5px 12px;
  border-radius: 20px;
  white-space: nowrap;
  pointer-events: none;
}

.waiting {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(238, 242, 248, 0.82);
  backdrop-filter: blur(3px);
  z-index: 5;
  font-size: 14px;
  color: var(--muted);
}

.waiting-spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2.5px solid #cfdaec;
  border-top-color: var(--brand);
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ============ 工具条 ============ */
.toolbar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 12px calc(10px + var(--safe-b));
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(14px) saturate(160%);
  border-top: 1px solid rgba(228, 234, 243, 0.9);
}

.tb-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}
.tb-row:first-child { justify-content: space-between; }
.tb-row:last-child { justify-content: space-between; }

.tb-right { display: flex; gap: 8px; }

.seg {
  display: inline-flex;
  padding: 3px;
  background: #e8eef8;
  border-radius: 11px;
  gap: 2px;
}

.seg button {
  font-size: 13.5px;
  padding: 8px 14px;
  border-radius: 8px;
  color: #536380;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
  white-space: nowrap;
}

.seg button.is-on {
  background: #fff;
  color: var(--brand-deep);
  font-weight: 600;
  box-shadow: 0 2px 6px -2px rgba(22, 32, 46, 0.25);
}

.seg--tools button { padding: 8px 16px; }

.tbtn {
  height: 36px;
  min-width: 44px;
  padding: 0 12px;
  border-radius: 10px;
  background: #fff;
  border: 1px solid var(--line);
  font-size: 13.5px;
  color: #4a5a72;
  display: grid;
  place-items: center;
  font-weight: 500;
}
.tbtn:active { background: #f2f6fd; }
#btnUndo { font-size: 20px; padding: 0 12px 2px; color: #4a5a72; }

.swatches { display: flex; gap: 10px; }

.swatches button {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--c);
  border: 2px solid #fff;
  box-shadow: 0 0 0 1.5px transparent, 0 2px 6px -2px rgba(22, 32, 46, 0.45);
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.swatches button.is-on {
  box-shadow: 0 0 0 2px var(--c), 0 2px 8px -2px rgba(22, 32, 46, 0.4);
  transform: scale(1.06);
}

.widths { display: flex; gap: 6px; }

.widths button {
  width: 38px;
  height: 34px;
  border-radius: 9px;
  background: #fff;
  border: 1px solid var(--line);
  display: grid;
  place-items: center;
}

.widths button i {
  display: block;
  width: 18px;
  border-radius: 6px;
  background: #b7c3d6;
}

.widths button.is-on { border-color: var(--brand); background: var(--brand-soft); }
.widths button.is-on i { background: var(--brand); }

/* 学生端：隐藏工具条，显示观看条 */
.studentbar { display: none; }

body.role-student .toolbar { display: none; }

body.role-student .studentbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 16px calc(16px + var(--safe-b));
  font-size: 13.5px;
  color: #4a5a72;
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(14px);
  border-top: 1px solid rgba(228, 234, 243, 0.9);
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #2ec27e;
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.8); }
}

/* ============ 邀请面板 ============ */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(16, 24, 36, 0.38);
}

.sheet[hidden] { display: none; }

.sheet-panel {
  width: 100%;
  max-width: 460px;
  background: #fff;
  border-radius: 22px 22px 0 0;
  padding: 24px 22px calc(22px + var(--safe-b));
  text-align: center;
  animation: rise 0.24s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes rise { from { transform: translateY(24px); } to { transform: translateY(0); } }

.sheet-panel h3 { margin: 0 0 16px; font-size: 17px; font-weight: 600; }

.sheet-code {
  font-size: 42px;
  font-weight: 700;
  letter-spacing: 8px;
  font-variant-numeric: tabular-nums;
  color: var(--brand-deep);
  padding-left: 8px;
}

.sheet-desc { font-size: 13px; color: var(--muted); margin: 8px 0 14px; }

.sheet-link {
  font-size: 12.5px;
  color: #42526b;
  background: #f4f7fc;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  word-break: break-all;
  margin-bottom: 18px;
  -webkit-user-select: text;
  user-select: text;
}

.sheet-actions { display: flex; gap: 10px; }
.sheet-actions .btn { flex: 1; min-height: 50px; align-items: center; }

/* ============ toast ============ */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translate(-50%, 12px);
  background: rgba(22, 32, 46, 0.9);
  color: #fff;
  font-size: 13.5px;
  padding: 10px 18px;
  border-radius: 22px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  z-index: 80;
  max-width: 80vw;
  text-align: center;
}

.toast.is-show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ============ iPad / 横屏 ============ */
@media (min-width: 760px) {
  .brand h1 { font-size: 31px; }
  .btn { min-height: 72px; }
  .toolbar {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 22px;
    padding: 10px 20px calc(12px + var(--safe-b));
  }
  .tb-row { width: auto; gap: 16px; }
  .board { padding: 14px 24px; }
  .code-cells span { height: 88px; font-size: 40px; }
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 22px; }
}

@media (min-width: 760px) and (orientation: landscape) {
  .board { padding: 10px 20px; }
}

@media (max-height: 520px) {
  .home { gap: 20px; padding-top: 16px; }
  .mark { width: 52px; height: 52px; font-size: 28px; margin-bottom: 12px; border-radius: 16px; }
  .brand h1 { font-size: 22px; }
  .brand p { font-size: 13px; }
  .btn { min-height: 56px; }
  .toolbar { gap: 6px; padding-top: 6px; }
}


/* ============ 手机横屏：压缩上下栏，给练习格更多高度 ============ */
@media (max-height: 500px) {
  .topbar {
    min-height: 44px;
    padding: 4px calc(8px + var(--safe-r)) 4px calc(8px + var(--safe-l));
  }
  .roomtag { padding: 4px 11px; border-radius: 10px; }
  .roomtag b { font-size: 17px; }
  .iconbtn { width: 30px; height: 30px; font-size: 22px; }
  .board { padding: 6px 12px; }
  .studentbar { padding: 8px 16px calc(10px + var(--safe-b)); font-size: 12.5px; }
  .toolbar { padding: 6px 12px calc(8px + var(--safe-b)); gap: 6px; }
  .seg button { padding: 6px 12px; font-size: 13px; }
  .tbtn { height: 32px; min-width: 38px; font-size: 13px; }
  .widths button { height: 30px; width: 34px; }
  .swatches button { width: 26px; height: 26px; }
  .swatches { gap: 8px; }
}
