:root {
  --accent: #cf5e53;
  --primary: #4a6b6e;
  --secondary: #f5f0d7;
  --paper: #f7f1de;
  --ink: #3a3a35;
  --ink-soft: #7a7a6e;
  --coral: #d96b5e;
  --teal: #4a9d92;
  --amber: #c99a3c;
  --plum: #9a6a9e;
  --brass: #c9a24b;
  --brass-light: #e3c97c;
  --brass-dark: #9c7a2c;
  --line: rgba(0,0,0,0.08);
  --key-radius: 46%;
  --serif: Georgia, 'Times New Roman', serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background:
    repeating-linear-gradient(0deg, rgba(0,0,0,0.04) 0 2px, transparent 2px 5px),
    linear-gradient(145deg, #b98d5a 0%, #a37647 45%, #8d6237 100%);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-tap-highlight-color: transparent;
}

body { overscroll-behavior-y: none; }

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  padding-bottom: calc(92px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(160deg, rgba(120,80,40,0.15), rgba(0,0,0,0.25));
  box-shadow: 0 0 60px rgba(0,0,0,0.35);
}

/* ---------- App bar ---------- */
.app-bar {
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 8px;
  background: linear-gradient(#3f5a5d, #37494b);
  box-shadow: 0 4px 10px rgba(0,0,0,0.3);
  position: relative;
  z-index: 3;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon {
  width: 24px; height: 24px; border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #a8463c);
  box-shadow: inset 0 1px 2px rgba(255,255,255,0.5), inset 0 -2px 3px rgba(0,0,0,0.35);
  position: relative;
}
.brand-icon::after {
  content: ""; position: absolute; inset: 6px; border-radius: 3px;
  background: var(--secondary);
  opacity: 0.85;
}
.brand-name { font-size: 22px; color: #f5f0d7; letter-spacing: 3px; font-weight: 700; }
.app-bar-rule {
  height: 3px; margin-top: 8px; border-radius: 2px;
  background: linear-gradient(90deg, var(--brass-light), var(--brass-dark));
}

/* ---------- Tab panes ---------- */
.tab-pane {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding: 14px 14px 20px;
}
.tab-pane::-webkit-scrollbar { display: none; }

/* ---------- Shared sheet (paper) ---------- */
.sheet {
  background:
    radial-gradient(circle at 85% 10%, rgba(0,0,0,0.04), transparent 40%),
    repeating-linear-gradient(0deg, rgba(70,70,60,0.06) 0 1px, transparent 1px 26px),
    var(--paper);
  border: 1px solid #d8cfb2;
  border-radius: 4px;
  padding: 18px 18px 22px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.5), 0 4px 12px rgba(0,0,0,0.18);
}
.sheet-heading {
  font-family: var(--serif);
  font-style: italic;
  font-size: 17px;
  color: var(--ink-soft);
  border-bottom: 1px solid rgba(0,0,0,0.12);
  padding-bottom: 8px;
  margin-bottom: 14px;
}

/* ---------- Forms ---------- */
.field { display: block; margin-bottom: 12px; border: 0; }
.field-label {
  display: block; font-size: 12px; letter-spacing: 2px;
  color: var(--ink-soft); margin-bottom: 5px; font-family: var(--serif);
}
.optional { color: #b0a888; }
input[type=text], input[type=search], textarea {
  width: 100%;
  font-family: "Courier New", Courier, monospace;
  font-size: 16px;
  color: var(--ink);
  background: rgba(255,255,255,0.55);
  border: 1px solid rgba(120,100,60,0.4);
  border-radius: 3px;
  padding: 10px 11px;
}
input:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(74,107,110,0.25);
  background: rgba(255,255,255,0.85);
}
textarea { resize: vertical; }

.form-error {
  color: #b03a2f; font-size: 13px; font-style: italic;
  margin: 2px 0 10px; background: rgba(207,94,83,0.1);
  border-radius: 3px; padding: 6px 9px; border: 1px solid rgba(207,94,83,0.3);
}

/* ---------- Language picker ---------- */
.lang-picker { display: flex; gap: 8px; }
.lang-key {
  flex: 1;
  font-family: var(--serif);
  font-size: 14px; font-weight: 700;
  padding: 11px 0;
  border-radius: var(--key-radius);
  border: 1px solid #b6ad8f;
  color: var(--ink);
  background: linear-gradient(#fff, #e8e0c6);
  box-shadow: 0 3px 0 #b6ad8f, inset 0 1px 1px rgba(255,255,255,0.8);
  transition: transform 0.06s, box-shadow 0.06s;
}
.lang-key:active { transform: translateY(2px); box-shadow: 0 1px 0 #b6ad8f; }
.lang-key.active {
  background: linear-gradient(#5a7d81, #466467);
  color: #fff; border-color: #2f4a4d;
  box-shadow: 0 2px 0 #2f4a4d, inset 0 1px 1px rgba(255,255,255,0.4);
  transform: translateY(1px);
}

/* ---------- Round typewriter keys ---------- */
.stamp-key, .danger-key {
  position: relative;
  font-family: var(--serif);
  border-radius: 50%;
  cursor: pointer;
  border: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.stamp-key .key-shine {
  position: absolute; left: 18%; top: 12%;
  width: 62%; height: 36%;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, rgba(255,255,255,0.65), transparent 65%);
  opacity: 0.7; pointer-events: none;
}
.stamp-key .key-label {
  position: relative; font-weight: 700;
  letter-spacing: 1px; text-align: center; line-height: 1.15;
  text-shadow: 0 1px 0 rgba(255,255,255,0.35);
}

/* --- primary key (coral default) --- */
.stamp-key {
  background: radial-gradient(circle at 40% 30%, #f0a89c, var(--coral) 55%, #b34b3f);
  color: #fff;
  box-shadow:
    0 6px 0 #9a4035,
    inset 0 2px 3px rgba(255,255,255,0.5),
    inset 0 -4px 6px rgba(0,0,0,0.25);
}
.stamp-key:active { transform: translateY(3px); box-shadow: 0 3px 0 #9a4035; }
.stamp-key:disabled {
  filter: grayscale(0.7) brightness(0.85);
  box-shadow: 0 4px 0 rgba(0,0,0,0.25);
  transform: translateY(2px);
  cursor: not-allowed;
}
.stamp-key.accent { background: radial-gradient(circle at 40% 30%, #efa89d, var(--accent) 55%, #a8463c); box-shadow: 0 5px 0 #8a3a31, inset 0 2px 3px rgba(255,255,255,0.5), inset 0 -4px 6px rgba(0,0,0,0.25); }
.stamp-key.accent:active { transform: translateY(2px); box-shadow: 0 3px 0 #8a3a31; }
.stamp-key.coral { background: radial-gradient(circle at 40% 30%, #f2a99e, var(--coral) 55%, #b34b3f); }
.stamp-key.amber { background: radial-gradient(circle at 40% 30%, #e8c57a, var(--amber) 55%, #9c6f1f); box-shadow: 0 5px 0 #80601e, inset 0 2px 3px rgba(255,255,255,0.5), inset 0 -4px 6px rgba(0,0,0,0.25); }

.stamp-key#stamp-key {
  width: 118px; height: 118px; font-size: 18px;
}
.stamp-key#new-word-key {
  width: 66px; height: 66px; font-size: 10px;
  margin-left: 6px;
}
.review-actions .stamp-key {
  width: 92px; height: 92px; font-size: 14px;
}
#save-key {
  width: 92px; height: 92px; font-size: 15px;
}

.danger-key {
  background: linear-gradient(#f0f0f0, #d6d6d6);
  color: #a03a2f;
  border: 1px solid #b6b0a0;
  padding: 11px 20px;
  font-weight: 700;
  border-radius: 14px;
  box-shadow: 0 3px 0 #b0aa9a;
}
.danger-key:active { transform: translateY(2px); box-shadow: 0 1px 0 #b0aa9a; }

.stamp-row {
  display: flex; justify-content: center; margin-top: 16px;
}

/* ---------- Dashboard / odometer ---------- */
.dashboard {
  background:
    radial-gradient(circle at 50% 120%, rgba(0,0,0,0.15), transparent 70%),
    linear-gradient(#b9b3a1, #a39d8b);
  border: 1px solid #8f8978;
  border-radius: 10px;
  padding: 14px 10px 10px;
  box-shadow: inset 0 1px 3px rgba(255,255,255,0.4), inset 0 -3px 6px rgba(0,0,0,0.25), 0 4px 10px rgba(0,0,0,0.2);
  position: relative;
}
.dashboard::before {
  content: ""; position: absolute; top: -4px; left: 6%; right: 6%; height: 4px;
  background: linear-gradient(#cfb873, #9c7a2c);
  border-radius: 3px; box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.odometer-row { display: flex; align-items: center; justify-content: space-around; }
.odometer { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.odometer-wheel {
  width: 58px; height: 40px;
  border-radius: 5px;
  background: linear-gradient(180deg, #5c5c52 0%, #3a3a33 40%, #2c2c26 100%);
  border: 2px solid #7d7a6c;
  box-shadow: inset 0 0 8px rgba(0,0,0,0.8), 0 3px 5px rgba(0,0,0,0.35);
  display: flex; align-items: center; justify-content: center;
  position: relative;
  overflow: hidden;
}
.odometer-wheel::before, .odometer-wheel::after {
  content: ""; position: absolute; top: -3px; bottom: -3px; width: 3px;
  background: linear-gradient(180deg, var(--brass-light), var(--brass-dark));
}
.odometer-wheel::before { left: 3px; }
.odometer-wheel::after { right: 3px; }
.od-num {
  font-family: "Courier New", Courier, monospace;
  font-size: 28px; font-weight: 700;
  color: #f5f0d7;
  text-shadow: 0 0 4px rgba(255,255,255,0.3);
  letter-spacing: 2px;
}
.odometer-label {
  font-size: 9px; letter-spacing: 2px; color: #4c4535;
  background: linear-gradient(#e2c878, #c9a24b);
  border: 1px solid #9c7a2c;
  border-radius: 4px;
  padding: 2px 6px;
  font-weight: 700;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.5);
}

/* ---------- Tape list ---------- */
.tape { display: flex; flex-direction: column; gap: 2px; margin-top: 16px; }
.strip {
  background: repeating-linear-gradient(0deg, rgba(70,70,60,0.05) 0 1px, transparent 1px 4px), var(--paper);
  border-top: 1px dashed #c9bf9f;
  border-bottom: 1px dashed #c9bf9f;
  padding: 10px 14px;
  display: flex; align-items: center; gap: 10px;
  cursor: pointer;
  position: relative;
  box-shadow: 0 2px 3px rgba(0,0,0,0.06);
  transition: transform 0.05s, box-shadow 0.05s;
  animation: tear 0.2s ease;
}
.strip:first-child { border-top: none; }
.strip:active { transform: translateY(1px); }
@keyframes tear { from { clip-path: inset(0 0 6px 0); } to { clip-path: inset(0 0 0 0); } }
.strip-body { flex: 1; min-width: 0; }
.strip-word {
  font-family: "Courier New", Courier, monospace;
  font-size: 18px; font-weight: 700;
  color: var(--ink);
  display: block;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.strip-trans {
  font-family: var(--serif);
  font-style: italic; font-size: 13px;
  color: var(--ink-soft);
  display: block; margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.strip-ink {
  width: 14px; height: 14px; border-radius: 50%;
  flex-shrink: 0;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.3), inset 0 -1px 1px rgba(255,255,255,0.4);
}
.empty-state {
  text-align: center; padding: 40px 20px; color: var(--ink-soft);
  font-style: italic;
}
.empty-state p { margin: 4px 0; }

/* ---------- Stack grid ---------- */
.search-row { margin-bottom: 12px; }
.search-input { font-size: 15px; }
.stack-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.stack-card {
  aspect-ratio: 3 / 4;
  background: linear-gradient(160deg, #f9f4e3, #e8dfc4);
  border: 1px solid #cfc49f;
  border-radius: 6px;
  padding: 8px;
  display: flex; align-items: flex-start; justify-content: center;
  text-align: center;
  cursor: pointer;
  box-shadow: inset 0 0 0 3px rgba(255,255,255,0.5), 0 3px 6px rgba(0,0,0,0.18);
  font-family: "Courier New", Courier, monospace;
  font-weight: 700; font-size: 13px;
  color: var(--ink);
  overflow-wrap: anywhere;
  word-break: break-word;
  position: relative;
}
.stack-card .stack-ink {
  position: absolute; bottom: 8px; right: 8px;
  width: 9px; height: 9px; border-radius: 50%;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
}
.stack-card:active { transform: translateY(1px); }

/* ---------- Review ---------- */
.review-top { text-align: center; margin-bottom: 14px; }
.review-counter {
  font-family: "Courier New", Courier, monospace;
  font-size: 14px; color: var(--ink);
  background: rgba(245,240,215,0.8);
  border: 1px solid rgba(120,100,60,0.4);
  border-radius: 20px; padding: 5px 14px;
  letter-spacing: 1px;
}
.review-stage { display: flex; flex-direction: column; align-items: center; }
.review-card {
  width: 100%;
  min-height: 240px;
  background: linear-gradient(160deg, #fbf6e7, #ece3c8);
  border: 1px solid #cfc49f;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: inset 0 0 0 4px rgba(255,255,255,0.5), 0 6px 14px rgba(0,0,0,0.2);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px;
  padding: 20px;
  font-family: inherit;
  color: var(--ink);
  transition: transform 0.25s;
}
.review-card.flipped { transform: rotateY(180deg); }
.review-word {
  font-family: "Courier New", Courier, monospace;
  font-size: 30px; font-weight: 700;
  overflow-wrap: anywhere;
}
.review-trans {
  font-family: var(--serif);
  font-style: italic; font-size: 22px;
  color: var(--ink-soft);
  overflow-wrap: anywhere;
}
.review-lang-note { font-size: 13px; color: var(--ink-soft); font-style: italic; }
.review-hint { font-size: 12px; color: #b0a888; font-style: italic; }
.review-actions {
  display: flex; gap: 24px;
  margin-top: 24px; justify-content: center;
}
.review-empty { text-align: center; padding: 30px; color: var(--ink-soft); }
.review-empty p { margin: 6px 0; font-style: italic; }
.review-empty-sub { font-size: 13px; }

/* ---------- Bottom tab bar ---------- */
.tab-bar {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 480px;
  z-index: 20;
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(#46595b, #2f3f41);
  border-top: 2px solid #6d8385;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.3);
}
.tab-key {
  position: relative;
  border: none;
  cursor: pointer;
  width: 66px;
  height: 62px;
  border-radius: var(--key-radius);
  font-family: var(--serif);
  display: flex; flex-direction: column;
  align-items: center; justify-content: flex-end;
  padding-bottom: 7px;
  gap: 3px;
  transition: transform 0.07s, box-shadow 0.07s;
}
.tab-key .key-shine {
  position: absolute; top: 10%; left: 20%; width: 60%; height: 30%;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, rgba(255,255,255,0.6), transparent 65%);
  pointer-events: none;
}
.tab-key .tab-label {
  font-size: 10px; font-weight: 700; letter-spacing: 0.5px;
  text-shadow: 0 1px 1px rgba(0,0,0,0.3);
}

/* inactive key colors */
.tab-key[data-tab="compose"] { background: radial-gradient(circle at 40% 30%, #e79a90, #c96458 55%, #a5493f); color: #fff; box-shadow: 0 5px 0 #8f4036, inset 0 2px 2px rgba(255,255,255,0.4); }
.tab-key[data-tab="tape"]    { background: radial-gradient(circle at 40% 30%, #7fb8ad, var(--teal) 55%, #2f746b); color: #fff; box-shadow: 0 5px 0 #225d55, inset 0 2px 2px rgba(255,255,255,0.4); }
.tab-key[data-tab="stack"]   { background: radial-gradient(circle at 40% 30%, #d9b55f, var(--amber) 55%, #9c6f1f); color: #fff; box-shadow: 0 5px 0 #7c5919, inset 0 2px 2px rgba(255,255,255,0.4); }
.tab-key[data-tab="review"]  { background: radial-gradient(circle at 40% 30%, #b88cbc, var(--plum) 55%, #7a4d7e); color: #fff; box-shadow: 0 5px 0 #634065, inset 0 2px 2px rgba(255,255,255,0.4); }

/* inactive: slightly lighter / matte */
.tab-key:not(.active) { filter: saturate(0.8) brightness(1.06); }

/* active: pressed down, glossy, typebar behind */
.tab-key.active {
  transform: translateY(3px);
  filter: saturate(1.1) brightness(1);
  box-shadow: 0 1px 0 rgba(0,0,0,0.4), inset 0 2px 4px rgba(0,0,0,0.35), inset 0 -1px 0 rgba(255,255,255,0.3);
}
.tab-key.active::before {
  content: "";
  position: absolute;
  left: 50%; bottom: calc(100% - 2px);
  transform: translateX(-50%) rotate(0deg);
  width: 6px; height: 14px;
  background: linear-gradient(#c9a24b, #9c7a2c);
  border: 1px solid #7d611f;
  border-radius: 2px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.tab-key.active .key-shine { opacity: 0.9; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; }
.modal-overlay { position: absolute; inset: 0; background: rgba(20,25,25,0.6); }
.modal-panel {
  position: relative;
  width: calc(100% - 40px);
  max-width: 400px;
  max-height: 86dvh;
  overflow-y: auto;
  z-index: 1;
}
.modal-actions {
  display: flex; align-items: center; justify-content: space-around;
  gap: 14px; margin-top: 8px;
}

.safe-spacer { height: 0; }

/* smaller screens */
@media (max-width: 380px) {
  .stack-grid { grid-template-columns: repeat(3, 1fr); }
  .tab-key { width: 60px; height: 58px; }
}