:root {
  --bg: #1b1410;
  --wood: #c98a4a;
  --wood-dark: #8a5324;
  --ink: #f4e9d8;
  --blue: #1e3fbf;
  --red: #c41e1e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Nanum Gothic", "Malgun Gothic", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 12px 16px 24px;
  min-height: 100vh;
}

#stage {
  position: relative;
  width: min(100%, 640px, calc((100vh - 200px) * 4 / 3));
  aspect-ratio: 4 / 3;
  max-width: 100%;
  border: 4px solid var(--wood-dark);
  border-radius: 6px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .6);
  background: #000;
  overflow: hidden;
}

#screen {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  outline: none;
  cursor: pointer;
}

/* ---------- 오버레이 (이름 입력 / 순위표) ---------- */
.overlay[hidden] { display: none; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .55);
  padding: 12px;
}

.panel {
  background: var(--wood);
  color: #2a1608;
  border: 3px solid var(--wood-dark);
  border-radius: 8px;
  padding: 16px 20px;
  width: min(100%, 440px);
  max-height: 100%;
  overflow: auto;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .5);
}

.panel h2 { margin: 0 0 10px; font-size: 1.3rem; }
.panel p { margin: 6px 0; }
.panel .hint { font-size: .8rem; opacity: .75; }
.panel .row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

#name-form { display: flex; gap: 8px; margin-top: 10px; }
#name-input {
  flex: 1;
  min-width: 0;
  font-size: 1.05rem;
  padding: 8px 10px;
  border: 2px solid var(--wood-dark);
  border-radius: 4px;
  background: #fff7ea;
}

#rank-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
#rank-table th, #rank-table td { padding: 4px 6px; border-bottom: 1px solid rgba(0, 0, 0, .2); text-align: left; }
#rank-table td:nth-child(3), #rank-table td:nth-child(4),
#rank-table th:nth-child(3), #rank-table th:nth-child(4) { text-align: right; }
#rank-table tr.me { background: rgba(255, 255, 255, .45); font-weight: bold; }

button {
  font: inherit;
  cursor: pointer;
  border: 2px solid var(--wood-dark);
  border-radius: 6px;
  background: #f4dcbd;
  color: #2a1608;
  padding: 8px 14px;
}
button:hover { background: #fff1de; }
button:active { transform: translateY(1px); }
button.danger { background: #f2b8b8; }

/* ---------- 터치 버튼 ---------- */
#touch-keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  width: min(100%, 640px);
}

#touch-keys button {
  padding: 12px 4px;
  font-weight: bold;
  font-size: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  user-select: none;
  touch-action: manipulation;
}
#touch-keys button small { font-weight: normal; opacity: .7; font-family: monospace; }
#touch-keys button.blue { background: #2c4fd6; color: #fff; border-color: #16257a; }
#touch-keys button.blue:hover { background: #3a5ee6; }
#touch-keys button.white { background: #fafafa; color: #222; border-color: #888; }
#touch-keys button.pressed { filter: brightness(.8); transform: translateY(2px); }

#options {
  display: flex;
  gap: 18px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  font-size: .95rem;
}
#options label { display: flex; align-items: center; gap: 6px; cursor: pointer; }

footer {
  font-size: .8rem;
  opacity: .7;
  text-align: center;
  max-width: 640px;
  line-height: 1.6;
}
kbd {
  font-family: monospace;
  background: #3a2a20;
  border: 1px solid #6b4d3a;
  border-radius: 3px;
  padding: 0 4px;
}

@media (max-width: 480px) {
  #touch-keys { grid-template-columns: repeat(2, 1fr); }
  #touch-keys button { font-size: .95rem; }
}

footer a { color: #f0b060; }

/* ---------- 프로그램 목록 (index.html) ---------- */
#launcher { width: min(100%, 720px); }
#launcher h1 { text-align: center; font-size: 1.6rem; margin: 8px 0 2px; color: #f4dcbd; }
#launcher .sub { text-align: center; opacity: .75; margin: 0 0 18px; font-size: .95rem; }
.programs { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 18px; }
.program-card {
  display: block;
  text-decoration: none;
  color: #2a1608;
  background: var(--wood);
  border: 3px solid var(--wood-dark);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
  transition: transform .12s, box-shadow .12s;
}
.program-card:hover, .program-card:focus-visible { transform: translateY(-3px); box-shadow: 0 12px 30px rgba(0, 0, 0, .6); outline: none; }
.program-card img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; image-rendering: pixelated; border-bottom: 3px solid var(--wood-dark); background: #000; }
.program-card .body { padding: 12px 14px 14px; }
.program-card h2 { margin: 0 0 6px; font-size: 1.2rem; }
.program-card p { margin: 0 0 8px; font-size: .9rem; line-height: 1.5; }
.program-card .meta { font-size: .8rem; opacity: .7; }
.program-card .best { font-size: .85rem; font-weight: bold; color: #7a1010; }

/* ---------- 퍼즐 방향 버튼 ---------- */
#dpad { display: grid; grid-template-columns: repeat(3, 64px); grid-template-rows: repeat(2, 56px); gap: 6px; justify-content: center; }
#dpad button { font-size: 1.4rem; padding: 0; }
#dpad [data-dir="up"] { grid-column: 2; grid-row: 1; }
#dpad [data-dir="left"] { grid-column: 1; grid-row: 2; }
#dpad [data-dir="down"] { grid-column: 2; grid-row: 2; }
#dpad [data-dir="right"] { grid-column: 3; grid-row: 2; }
#puzzle-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
select { font: inherit; padding: 4px 6px; border-radius: 4px; border: 2px solid var(--wood-dark); background: #f4dcbd; color: #2a1608; }

/* ---------- 텍스트 화면 (textscreen.js: 바이오리듬, 계산 연습) ---------- */
.text-stage #screen { image-rendering: auto; cursor: text; }
.kbd-sink {
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  font-size: 16px;   /* iOS가 입력창을 확대하지 않도록 */
  pointer-events: none;
}
.summary { margin: 0; min-height: 1.4em; text-align: center; font-size: .92rem; max-width: 640px; line-height: 1.5; }

.paper-wrap { width: min(100%, 760px); background: #fbf8ef; color: #222; border-radius: 6px; padding: 10px 12px; box-shadow: 0 6px 20px rgba(0, 0, 0, .5); }
.paper-wrap .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.paper-wrap .spacer { flex: 1; }
#paper { margin: 0; overflow-x: auto; font-family: "D2Coding", "Nanum Gothic Coding", "Noto Sans Mono CJK KR", monospace; font-size: 12px; line-height: 1.25; }

@media print {
  body * { visibility: hidden; }
  html, body { background: #fff; }
  #paper, #paper * { visibility: visible; color: #000; }
  #paper { position: absolute; left: 0; top: 0; overflow: visible; font-size: 10px; }
}

/* ---------- 계산 연습 ---------- */
.prog-tabs { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.prog-tabs button[aria-pressed="true"] { background: var(--wood); font-weight: bold; }
.status { font-size: .9rem; opacity: .85; text-align: center; min-height: 1.3em; }

/* ---------- 그림판 (원본 픽셀 폭 그대로 보이도록 테두리만큼 넓힌다) ---------- */
#stage.paint-stage { width: min(100%, 728px, calc((100vh - 230px) * 4 / 3)); aspect-ratio: 728 / 548; }

/* ---------- 그림판 ---------- */
.paint-stage #screen { cursor: crosshair; touch-action: none; }
.paint-bar { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; width: min(100%, 720px); }
.paint-bar button { padding: 6px 10px; font-size: .92rem; }
.paint-status { font-family: monospace; font-size: .85rem; opacity: .85; text-align: center; min-height: 1.3em; }
.file-list { list-style: none; margin: 8px 0; padding: 0; max-height: 220px; overflow: auto; }
.file-list li { display: flex; gap: 6px; align-items: center; padding: 4px 0; border-bottom: 1px solid rgba(0, 0, 0, .2); }
.file-list li span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-list li small { opacity: .7; }
.panel label.block { display: block; margin: 6px 0; }
#panel-save input[type="text"], .file-list input[type="text"] { font: inherit; padding: 6px 8px; border: 2px solid var(--wood-dark); border-radius: 4px; background: #fff7ea; width: 100%; }

/* ---------- 계산기 (calcu/) 편집 키 ---------- */
.key-row { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; width: min(100%, 640px); }
.key-row button { padding: 6px 10px; min-width: 2.6em; font-size: .95rem; font-family: monospace; }
#options input[type="number"] { font: inherit; padding: 4px 6px; border-radius: 4px; border: 2px solid var(--wood-dark); background: #f4dcbd; color: #2a1608; }
footer code { font-family: monospace; background: #3a2a20; border-radius: 3px; padding: 0 4px; }
