:root {
	--bg: #f6f6f4;
	--surface: #ffffff;
	--text: #1c1c1a;
	--text-muted: #6f6e69;
	--border: #e3e2dd;
	--accent: #4f46e5;
	--accent-hover: #4338ca;
	--danger: #dc2626;
	--warning: #d97706;
	--success: #16a34a;
	--info: #2563eb;
	--badge-due-bg: #fee2e2;
	--badge-due-text: #991b1b;
	--badge-new-bg: #dbeafe;
	--badge-new-text: #1e40af;
	--mark-bg: #fef08a;
	--radius: 10px;
}

:root[data-theme='dark'] {
	--bg: #191918;
	--surface: #262624;
	--text: #ecebe6;
	--text-muted: #a3a29b;
	--border: #3a3a37;
	--accent: #6366f1;
	--accent-hover: #818cf8;
	--danger: #f87171;
	--warning: #fbbf24;
	--success: #4ade80;
	--info: #60a5fa;
	--badge-due-bg: #45201f;
	--badge-due-text: #fca5a5;
	--badge-new-bg: #1e2f4a;
	--badge-new-text: #93c5fd;
	--mark-bg: #b59f3b;
}

* { box-sizing: border-box; }

body {
	margin: 0;
	background: var(--bg);
	color: var(--text);
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
	line-height: 1.55;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; font-weight: 650; }
h2 { font-size: 1.15rem; font-weight: 600; }

.container {
	max-width: 760px;
	margin: 0 auto;
	padding: 1.5rem max(1rem, env(safe-area-inset-right)) max(4rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}

@media (max-width: 640px) {
	main.container { padding-bottom: calc(4.5rem + env(safe-area-inset-bottom)); }
}

.card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	padding: 1rem 1.25rem;
}

.btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 0.4rem;
	padding: 0.5rem 1rem;
	border: 1px solid var(--border);
	border-radius: var(--radius);
	background: var(--surface);
	color: var(--text);
	font-size: 0.95rem;
	font-weight: 500;
	cursor: pointer;
	transition: background 0.12s ease;
}
.btn:hover { background: var(--bg); }
.btn-primary {
	background: var(--accent);
	border-color: var(--accent);
	color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost {
	border-color: transparent;
	background: transparent;
	color: var(--text-muted);
}

input, select, textarea {
	width: 100%;
	padding: 0.55rem 0.75rem;
	border: 1px solid var(--border);
	border-radius: var(--radius);
	background: var(--surface);
	font: inherit;
	color: var(--text);
}
input:focus, textarea:focus {
	outline: 2px solid var(--accent);
	outline-offset: -1px;
	border-color: transparent;
}

label {
	display: block;
	font-size: 0.85rem;
	font-weight: 500;
	color: var(--text-muted);
	margin-bottom: 0.25rem;
}
.field { margin-bottom: 0.9rem; }
.error { color: var(--danger); font-size: 0.9rem; }
.muted { color: var(--text-muted); }

.badge {
	display: inline-block;
	padding: 0.1rem 0.5rem;
	border-radius: 999px;
	font-size: 0.78rem;
	font-weight: 600;
}
.badge-due { background: var(--badge-due-bg); color: var(--badge-due-text); }
.badge-new { background: var(--badge-new-bg); color: var(--badge-new-text); }
.badge-total { background: var(--bg); color: var(--text-muted); }

/* --- Header / Nav --- */
header {
	border-bottom: 1px solid var(--border);
	background: var(--surface);
	position: sticky;
	top: 0;
	z-index: 50;
}
header .container {
	display: flex;
	align-items: center;
	padding-top: 0;
	padding-bottom: 0;
	padding: 0.6rem max(1rem, env(safe-area-inset-right)) 0.6rem max(1rem, env(safe-area-inset-left));
}

.nav { display: flex; align-items: center; width: 100%; }
.brand { font-weight: 700; font-size: 1.1rem; margin-right: 1.5rem; color: var(--text); }
.brand:hover { text-decoration: none; }

.nav-links { display: flex; gap: 0.25rem; }
.nav-links a {
	padding: 0.35rem 0.7rem;
	border-radius: var(--radius);
	color: var(--text-muted);
	font-size: 0.9rem;
}
.nav-links a:hover { background: var(--bg); text-decoration: none; }
.nav-links a.active { color: var(--text); font-weight: 600; background: var(--bg); }
.nav-right { margin-left: auto; display: flex; align-items: center; gap: 0.5rem; }
.email { font-size: 0.8rem; color: var(--text-muted); }

@media (max-width: 640px) {
	.nav-links { display: none; }
	.email { display: none; }
	.logout-text { display: none; }
	.logout-icon { display: inline; }
}
@media (min-width: 641px) {
	.logout-text { display: inline; }
	.logout-icon { display: none; }
}

/* Tabbar (mobile) */
.tabbar {
	display: none;
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: var(--surface);
	border-top: 1px solid var(--border);
	z-index: 50;
	padding-bottom: env(safe-area-inset-bottom);
}
.tabbar-inner {
	display: flex;
	justify-content: space-around;
	padding: 0.4rem 0;
}
.tabbar a {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.15rem;
	padding: 0.3rem 0.8rem;
	border-radius: var(--radius);
	color: var(--text-muted);
	font-size: 0.7rem;
}
.tabbar a.active { color: var(--accent); font-weight: 600; }
.tabbar a:hover { background: var(--bg); text-decoration: none; }
.tab-icon { font-size: 1.2rem; line-height: 1; }

@media (max-width: 640px) {
	.tabbar { display: block; }
}

/* --- Home page --- */
.head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.head h1 { margin: 0; }
.deck-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.75rem; }
.deck { display: flex; align-items: center; justify-content: space-between; }
.deck-info { display: flex; flex-direction: column; gap: 0.3rem; }
.deck-name { font-weight: 600; font-size: 1.05rem; }
.deck-actions { display: flex; align-items: center; gap: 0.5rem; }
.badges { display: flex; gap: 0.35rem; flex-wrap: wrap; }
.empty { text-align: center; padding: 2rem; }

/* --- Dictionary --- */
.search { margin-bottom: 1rem; }
.entry { margin-bottom: 0.75rem; }
.entry-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.3rem; }
.entry-word { font-weight: 600; }
.audio { cursor: pointer; background: none; border: none; font-size: 1.1rem; padding: 0; }
.translations { margin: 0.3rem 0; }
.glosses { font-size: 0.9rem; color: var(--text-muted); }
.add-form { margin-top: 0.5rem; display: flex; gap: 0.5rem; align-items: end; flex-wrap: wrap; }
.added { color: var(--success); font-weight: 500; }

/* --- Stats --- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; margin-bottom: 1rem; }
.tile { text-align: center; }
.tile-num { font-size: 1.8rem; font-weight: 700; }

.heatmap-card { overflow-x: auto; }
.heatmap-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.legend { display: flex; align-items: center; gap: 0.25rem; font-size: 0.75rem; color: var(--text-muted); }
.legend .cell { display: inline-block; width: 12px; height: 12px; border-radius: 2px; }

.heatmap-scroll { overflow-x: auto; }
.heatmap { display: flex; gap: 0; }
.dow-col { display: flex; flex-direction: column; gap: 2px; margin-right: 4px; }
.dow-label { height: 12px; font-size: 0.65rem; color: var(--text-muted); line-height: 12px; }
.grid-wrap { }
.months { display: flex; gap: 2px; margin-bottom: 2px; height: 14px; }
.month-label { font-size: 0.65rem; color: var(--text-muted); white-space: nowrap; }
.grid { display: flex; gap: 2px; }
.week { display: flex; flex-direction: column; gap: 2px; }
.cell { width: 12px; height: 12px; border-radius: 2px; background: var(--border); }
.cell.lvl1 { background: color-mix(in srgb, var(--accent) 20%, var(--surface)); }
.cell.lvl2 { background: color-mix(in srgb, var(--accent) 40%, var(--surface)); }
.cell.lvl3 { background: color-mix(in srgb, var(--accent) 65%, var(--surface)); }
.cell.lvl4 { background: var(--accent); }
.cell.future { background: transparent; border: 1px solid var(--border); }

.history { display: flex; flex-direction: column; gap: 0.5rem; }
.hist-item { display: flex; justify-content: space-between; align-items: center; }
.hist-word { font-weight: 600; }
.hist-meta { font-size: 0.8rem; color: var(--text-muted); }
.hist-src { font-size: 0.8rem; color: var(--text-muted); }

/* --- Deck page --- */
.limit-row { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 1rem; }
.limit-row input { width: 70px; }
.limit-note { font-size: 0.8rem; color: var(--text-muted); }

.add-form .row { display: flex; gap: 0.5rem; }
.add-form .row .field { flex: 1; }
.hint { font-size: 0.75rem; color: var(--text-muted); margin-top: 0.2rem; }

.list-head { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.75rem; }
.list-head h2 { margin: 0; }
.note-search { flex: 1; }

.note-list { display: flex; flex-direction: column; gap: 0.5rem; }
.note { }
.note-main { display: flex; justify-content: space-between; align-items: center; }
.note-word { font-weight: 600; }
.note-tr { color: var(--text-muted); }
.note-audio { cursor: pointer; background: none; border: none; font-size: 1rem; padding: 0; }
.sentence { font-size: 0.9rem; color: var(--text-muted); margin-top: 0.25rem; }
.source { font-size: 0.8rem; }
.source a { color: var(--text-muted); }

.note-actions { display: flex; gap: 0.25rem; }
.edit-form { margin-top: 0.5rem; }
.edit-row { display: flex; gap: 0.5rem; }
.edit-row .field { flex: 1; }
.edit-actions { display: flex; gap: 0.5rem; margin-top: 0.3rem; }

/* --- Review --- */
.review-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.counter { font-size: 0.9rem; color: var(--text-muted); }
.autoplay { display: flex; align-items: center; gap: 0.35rem; font-size: 0.85rem; color: var(--text-muted); }

.review { text-align: center; padding: 2rem 1.5rem; }
.word { font-size: 2rem; font-weight: 700; margin-bottom: 0.5rem; }
.sentence { font-size: 1rem; color: var(--text-muted); margin-bottom: 0.5rem; }
mark { background: var(--mark-bg); padding: 0.1rem 0.2rem; border-radius: 3px; }

.show { margin-top: 1rem; }
.translation { font-size: 1.3rem; margin-bottom: 1rem; }

.answers { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin-top: 1rem; }
.answer { display: flex; flex-direction: column; align-items: center; gap: 0.2rem; padding: 0.75rem; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); cursor: pointer; transition: background 0.12s; }
.answer:hover { background: var(--bg); }
.answer-key { font-weight: 700; font-size: 0.8rem; }
.answer-label { font-weight: 600; }
.answer-interval { font-size: 0.75rem; color: var(--text-muted); }
.again { border-color: var(--danger); color: var(--danger); }
.hard { border-color: var(--warning); color: var(--warning); }
.good { border-color: var(--success); color: var(--success); }
.easy { border-color: var(--info); color: var(--info); }

.kbd { display: inline-flex; align-items: center; justify-content: center; min-width: 1.6rem; height: 1.6rem; padding: 0 0.3rem; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); font-size: 0.75rem; font-weight: 600; }

.done { text-align: center; padding: 2rem; }
.done-emoji { font-size: 3rem; margin-bottom: 0.5rem; }

.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 0.75rem; }
.s-tile { text-align: center; }
.s-num { font-size: 1.5rem; font-weight: 700; }
.breakdown { display: flex; justify-content: center; gap: 1rem; margin-top: 0.5rem; font-size: 0.9rem; color: var(--text-muted); }

/* --- Auth --- */
.auth { max-width: 380px; margin: 2rem auto; }
.auth h1 { text-align: center; }
.switch { text-align: center; margin-top: 0.75rem; font-size: 0.9rem; color: var(--text-muted); }
.switch .link { color: var(--accent); cursor: pointer; font-weight: 500; }
.switch .link:hover { text-decoration: underline; }

.pending { text-align: center; padding: 2rem; }
.pending-emoji { font-size: 3rem; margin-bottom: 0.5rem; }
.small { font-size: 0.85rem; color: var(--text-muted); }
.ok { color: var(--success); font-size: 0.9rem; }
.back { text-align: center; margin-top: 1rem; }

/* --- Misc --- */
.mt-1 { margin-top: 1rem; }
.mb-1 { margin-bottom: 1rem; }
.flex { display: flex; }
.flex-wrap { flex-wrap: wrap; }
.gap-1 { gap: 1rem; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.text-center { text-align: center; }
