:root {
    --bg: #121213;
    --bg-deep: #050505;
    --surface: #1d1d1f;
    --surface-soft: rgba(255, 255, 255, 0.07);
    --text: #ffffff;
    --muted: rgba(255, 255, 255, 0.55);
    --border: rgba(255, 255, 255, 0.12);
    --tile-border: #3a3a3c;
    --correct: #538d4e;
    --present: #b59f3b;
    --absent: #3a3a3c;
    --danger: #9b2335;
    --correct-glow: rgba(83, 141, 78, 0.35);
    --present-glow: rgba(181, 159, 59, 0.35);
    --header-h: 62px;
}

* { box-sizing: border-box; font-family: 'Almarai', sans-serif; letter-spacing: 0; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; }
button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
body {
    color: var(--text);
    background: linear-gradient(180deg, var(--bg) 0%, var(--bg-deep) 100%);
    display: flex;
    flex-direction: column;
    touch-action: manipulation;
}

body.light {
    --bg: #f5f5f3;
    --bg-deep: #e7e7e3;
    --surface: #ffffff;
    --surface-soft: rgba(0, 0, 0, 0.055);
    --text: #191919;
    --muted: rgba(0, 0, 0, 0.55);
    --border: rgba(0, 0, 0, 0.13);
    --tile-border: #a7a7a7;
    --absent: #787c7e;
}

.app-header {
    min-height: var(--header-h);
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(16px);
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    z-index: 20;
}

.header-side { display: flex; align-items: center; gap: 6px; min-width: 0; position: relative; }
.header-start { justify-content: flex-start; }
.header-end { justify-content: flex-start; direction: ltr; }
.icon-button {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface-soft);
    color: var(--text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    cursor: pointer;
    flex: 0 0 auto;
    font-size: 1rem;
}
.header-start .icon-button[href="index.html"]{background:linear-gradient(135deg,#538d4e,#3a6a36);color:white;border-color:rgba(83,141,78,.5);box-shadow:0 3px 12px rgba(83,141,78,.35);}
.icon-button:active { transform: scale(0.95); }

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; direction: ltr; padding:4px 8px; }
.brand-grid { width: 28px; height: 28px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; flex: 0 0 auto; }
.brand-grid span { border: 2px solid #4a4a4c; border-radius: 4px; background: #222224; }
.brand-grid .correct { border-color: #53BA67; background: #53BA67; box-shadow:0 0 10px rgba(83,186,103,.45); }
.brand-grid .present { border-color: #CBB03F; background: #CBB03F; box-shadow:0 0 10px rgba(203,176,63,.45); }
.brand-copy { display: flex; align-items: center; direction: rtl; white-space: nowrap; }
.brand-copy strong { font-size: clamp(20px,4vw,26px); font-weight: 800; line-height:1; }

.settings-menu {
    position: absolute;
    top: 48px;
    left: 0;
    width: 168px;
    padding: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 8px;
    box-shadow: 0 18px 42px rgba(0, 0, 0, 0.4);
    direction: rtl;
}
.settings-menu button { width: 100%; height: 42px; padding: 0 10px; border: 0; border-radius: 6px; background: transparent; display: flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 700; }
.settings-menu button:hover { background: var(--surface-soft); }
.settings-divider{margin:5px 6px 2px;padding-top:8px;border-top:1px solid var(--border);color:var(--muted);font-size:.63rem;font-weight:800;text-align:right;direction:rtl;}
.settings-menu .length-option.active{color:var(--correct);background:rgba(83,141,78,.1);}
.settings-menu .length-option:disabled{opacity:.42;cursor:not-allowed;}

.attempt-selector {
    width: min(100%, 330px);
    min-height: 36px;
    margin: 0 auto;
    padding: 3px 4px 3px 10px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-soft);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.attempt-selector-label { color: var(--muted); font-size: .68rem; font-weight: 800; white-space: nowrap; }
.attempt-selector-options { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; direction: rtl; }
.attempt-option {
    min-width: 88px;
    height: 28px;
    padding: 0 9px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--muted);
    font-size: .67rem;
    font-weight: 800;
    cursor: pointer;
    white-space: nowrap;
}
.attempt-option.active { background: var(--correct); color: white; box-shadow: 0 2px 9px var(--correct-glow); }
.attempt-option:disabled { opacity: .42; cursor: not-allowed; }

.game-shell {
    width: min(100%, 800px);
    min-height: 0;
    flex: 1;
    margin: 0 auto;
    padding: 6px 10px max(8px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-rows: 38px 32px minmax(0, 1fr) auto;
    align-items: center;
    overflow: hidden;
}
.message { min-height: 32px; display: flex; align-items: center; justify-content: center; padding: 0 10px; text-align: center; font-size: 0.85rem; font-weight: 800; color: var(--present); }

.board {
    --tile-size: 62px;
    --code-length: 5;
    display: flex;
    flex-direction: column;
    align-self: center;
    direction: ltr;
}
.number-grid-row {
    display: flex;
    justify-content: center;
    gap: 6px;
    margin-bottom: 5px;
}
.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    border: 2px solid var(--tile-border);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text);
    font-size: calc(var(--tile-size) * 0.52);
    line-height: 1;
    font-weight: 800;
    user-select: none;
    transform-style: preserve-3d;
}
.tile.filled { border-color: color-mix(in srgb, var(--text) 35%, transparent); animation: pop 120ms ease; }
.tile.correct, .example-tile.correct { background: var(--correct); border-color: var(--correct); color: white; box-shadow: 0 0 14px var(--correct-glow); }
.tile.present, .example-tile.present { background: var(--present); border-color: var(--present); color: white; box-shadow: 0 0 14px var(--present-glow); }
.tile.absent, .example-tile.absent { background: var(--absent); border-color: var(--absent); color: white; }
.tile.reveal { animation: flip 600ms ease; }
.number-grid-row.shake { animation: shake 380ms ease; }

.keyboard { width: min(100%, 560px); margin: 6px auto 0; direction: ltr; }
.keyboard-row { display: flex; gap: 6px; margin-top: 6px; }
.key {
    height: clamp(44px, 6.5vh, 58px);
    min-width: 0;
    flex: 1;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-soft);
    font-size: clamp(1rem, 4vw, 1.25rem);
    font-weight: 800;
    cursor: pointer;
    user-select: none;
}
.key:active, .key.pressed { transform: translateY(1px); filter: brightness(1.25); }
.key.correct { background: var(--correct); border-color: var(--correct); color: white; box-shadow: 0 0 8px var(--correct-glow); }
.key.present { background: var(--present); border-color: var(--present); color: white; box-shadow: 0 0 8px var(--present-glow); }
.key.absent { background: var(--absent); border-color: var(--absent); color: rgba(255,255,255,0.4); }
.key.action { flex: 1.8; display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 0.85rem; }
.key.enter { background: var(--correct); border-color: var(--correct); color: white; }
.key.backspace { background: var(--danger); border-color: var(--danger); color: white; }

.modal-overlay { position: fixed; inset: 0; z-index: 100; padding: 16px; background: rgba(0, 0, 0, 0.82); backdrop-filter: blur(12px); display: flex; align-items: center; justify-content: center; }
.modal-overlay[hidden] { display: none; }
.modal { width: 100%; max-width:430px; max-height:85dvh; overflow: auto; position: relative; padding: 32px 24px 26px; border: 1px solid rgba(255,255,255,.08); border-top-color:rgba(255,255,255,.15); border-radius: 28px; background: linear-gradient(145deg,rgba(30,30,32,.72),rgba(15,15,16,.96)); box-shadow: 0 30px 60px rgba(0,0,0,.75), inset 0 1px 0 rgba(255,255,255,.1); color:white; }
.modal-close { position: absolute; top: 13px; right: 13px; left:auto; width: 30px; height: 30px; border: 1px solid rgba(255,255,255,.06); border-radius: 9px; background: rgba(255,255,255,.05); color:rgba(255,255,255,.45); cursor: pointer; }
.modal-icon { width: 56px; height: 56px; margin: 0 auto 16px; border-radius: 14px; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg,#1e1e20,#0a0a0b); border:1px solid rgba(255,255,255,.1); color: var(--correct); font-size: 1.5rem; box-shadow:0 10px 25px rgba(0,0,0,.55),inset 0 2px 4px rgba(255,255,255,.05); }
.modal-icon.gold { color: var(--present); background: rgba(181, 159, 59, 0.14); }
.modal h2 { margin: 8px 0 12px; text-align: center; font-size: 1.5rem; }
.modal-intro, .result-copy { color: var(--muted); text-align: center; font-size: 0.87rem; line-height: 1.8; }
.primary-button, .secondary-button { min-height: 48px; border-radius: 13px; padding: 13px; font-size:.95rem;font-weight: 800; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 7px; }
.primary-button { width: 100%; border: 1px solid rgba(255,255,255,.1); background: linear-gradient(135deg,#538d4e,#3a6336); color: white; box-shadow:0 6px 18px rgba(83,141,78,.25); }
.secondary-button { border: 1px solid rgba(255,255,255,.08); background: rgba(255,255,255,.04); color: rgba(255,255,255,.85); }

.stats-subtitle{margin:-5px 0 15px;text-align:center;color:var(--muted);font-size:.68rem;font-weight:700;}
.played-stat-box{width:65%;min-height:94px;margin:0 auto 10px;border:1px solid rgba(212,175,55,.2);border-radius:14px;background:linear-gradient(180deg,rgba(255,255,255,.06),rgba(0,0,0,.2));display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center;box-shadow:inset 0 2px 8px rgba(0,0,0,.2);}
.played-stat-box span{color:var(--muted);font-size:.68rem;font-weight:800;}
.played-stat-box strong{font-size:2.65rem;line-height:1;margin-top:8px;}
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; margin: 0 0 18px; }
.stats-grid div { min-width: 0; min-height: 84px; padding: 10px 4px; border: 1px solid rgba(255,255,255,.06); border-radius: 14px; background: rgba(255,255,255,.04); text-align: center; display:flex;flex-direction:column;align-items:center;justify-content:center; box-shadow:inset 0 2px 8px rgba(0,0,0,.2); }
.stats-grid strong { display: block; margin-top:8px; font-size: 1.45rem; line-height:1; }
.stats-grid span { display:flex;align-items:center;justify-content:center;min-height:2.1rem;color:var(--muted);font-size: 0.59rem;line-height:1.45;font-weight: 700; }
.stats-grid div:first-child strong{color:var(--correct);}
.stats-grid div:nth-child(2) strong{color:var(--present);}
.history-panel{margin-top:4px;border:1px solid rgba(255,255,255,.04);border-radius:18px;background:rgba(0,0,0,.4);overflow:hidden;box-shadow:inset 0 4px 15px rgba(0,0,0,.3);}
.history-heading{padding:11px 16px;border-bottom:1px solid rgba(255,255,255,.05);display:flex;align-items:center;justify-content:space-between;font-size:.7rem;color:rgba(255,255,255,.45);font-weight:800;}
.history-heading i{color:rgba(255,255,255,.3);font-size:.75rem;}
.history-empty{padding:28px 12px;text-align:center;color:rgba(255,255,255,.4);font-size:.82rem;font-weight:700;}
.history-scroll{max-height:190px;overflow-y:auto;padding:8px 14px;}
.history-table{width:100%;border-collapse:separate;border-spacing:0;font-size:.82rem;}
.history-table th{padding:9px 6px;font-size:.68rem;opacity:.45;border-bottom:1px solid rgba(255,255,255,.06);}
.history-table td{padding:9px 6px;text-align:center;font-weight:700;color:rgba(255,255,255,.8);border-bottom:1px solid rgba(255,255,255,.04);}
.history-result.win{color:#74b86d;}.history-result.loss{color:#e74c3c;}
.result-details{margin:12px 0 16px;padding:16px;border:1px solid rgba(255,255,255,.04);border-radius:18px;background:rgba(0,0,0,.4);box-shadow:inset 0 4px 15px rgba(0,0,0,.3);text-align:center;}
.result-details span{display:block;color:rgba(255,255,255,.45);font-size:.7rem;font-weight:800;margin-bottom:7px;}
.result-details strong{display:block;color:white;font-size:1.8rem;font-weight:900;font-variant-numeric:tabular-nums;}
.result-matrix{display:flex;flex-direction:column;align-items:center;gap:5px;margin:0 auto 18px;}
.result-matrix-row{display:flex;direction:ltr;gap:5px;}
.result-matrix-tile{width:34px;height:34px;border-radius:6px;display:flex;align-items:center;justify-content:center;color:white;font-size:.9rem;font-weight:800;}
.result-matrix-tile.correct{background:var(--correct);}
.result-matrix-tile.present{background:var(--present);}
.result-matrix-tile.absent{background:var(--absent);color:rgba(255,255,255,.55);}
.result-actions { display: grid; grid-template-columns: 1fr 1.25fr; gap: 10px; }

@keyframes pop { 50% { transform: scale(1.1); } }
@keyframes flip { 0% { transform: rotateX(0); } 48% { transform: rotateX(90deg); } 52% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake { 20% { transform: translateX(-7px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(5px); } }

@media (max-width: 520px) {
    :root { --header-h: 54px; }
    .app-header { padding:10px; }
    .icon-button { width: 34px; height: 34px;border-radius:10px;font-size:.82rem; }
    .header-side { gap: 4px; }
    .brand { gap: 6px; }
    .brand-grid { width: 26px; height: 26px; gap: 2px; }
    .brand-copy strong { font-size: 18px; }
    .game-shell { padding-inline: 8px; }
    .keyboard-row { gap: 4px; margin-top: 4px; }
}

@media (max-width: 360px) {
    .brand-copy strong { font-size: 1.05rem; }
    .modal { padding-inline: 16px; }
    .stats-grid span{font-size:.55rem;}
    .result-matrix-tile{width:30px;height:30px;font-size:.8rem;}
    .attempt-selector { width: 100%; padding-left: 7px; gap: 5px; }
    .attempt-option { min-width: 78px; padding-inline: 6px; font-size: .62rem; }
}

@media (max-height: 680px) {
    .game-shell { padding-top: 4px; }
    .key { height: 42px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
