:root {
    --fs: 18px;
    --lh: 32px;
    --paper-bg: #fcf9e8;
    --line-color: #e0d9b4;
}

* { box-sizing: border-box; }
body, html { 
    margin: 0 !important; background-color: #2b2b2b !important; 
    height: 100dvh !important; font-family: sans-serif; overflow: hidden !important; 
}

.app-container { display: flex; flex-direction: column; width: 100%; max-width: 800px; height: 100%; margin: 0 auto; padding: 10px; }

.header { display: flex; justify-content: space-between; align-items: center; padding: 5px 0 10px 0; color: white; flex-shrink: 0; flex-wrap: wrap; gap: 8px;}
.header h3 { margin: 0; font-size: 20px; letter-spacing: 1px;}
.header-buttons { display: flex; gap: 6px; flex-wrap: wrap; }

.toggle-btn {
    background: #4a86e8; color: white; border: none; padding: 8px 12px; 
    border-radius: 6px; font-weight: bold; cursor: pointer; font-size: 14px;
    display: flex; align-items: center; justify-content: center; gap: 5px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3); transition: 0.2s;
}
.toggle-btn:hover { background: #3b71c6; }
.btn-icon { font-size: 16px; }
.btn-danger { background: #d32f2f; }
.btn-danger:hover { background: #b71c1c; }

.tabs-container { display: flex; gap: 4px; margin-bottom: 0px; }
.tab {
    background: #444; color: #aaa; padding: 8px 20px; border-radius: 8px 8px 0 0;
    cursor: pointer; font-size: 14px; font-weight: bold; border-bottom: none; transition: 0.2s;
}
.tab:hover { background: #555; }
.tab.active { background: var(--paper-bg); color: #333; }

.paper-container {
    position: relative; flex-grow: 1; background-color: var(--paper-bg);
    background-image: repeating-linear-gradient(transparent, transparent calc(var(--lh) - 1px), var(--line-color) calc(var(--lh) - 1px), var(--line-color) var(--lh));
    box-shadow: 0 0 20px rgba(0,0,0,0.4); border-radius: 0 8px 8px 8px; overflow: hidden; 
    margin-bottom: 10px; min-height: 0; transition: background-image 0.2s;
}
.paper-container.no-lines { background-image: none; background-color: #ffffff; }

.paper-container::before {
    content: ''; position: absolute; top: 0; left: 45px; bottom: 0;
    border-left: 1px solid #7bc384; border-right: 1px solid #7bc384; width: 4px; z-index: 0;
}

.backdrop { 
    position: absolute; left: 0; top: 0; width: 100%; height: 100%;
    padding: 4px 10px 4px 55px; z-index: 1; pointer-events: auto; color: #000;
}

.editor { 
    position: absolute; left: 45px; top: 0; width: calc(100% - 45px); height: 100%;
    padding: 4px 10px 4px 10px; z-index: 2; color: transparent; background: transparent; 
    caret-color: #000; resize: none; border: none; outline: none;
}

.editor, .backdrop {
    font-family: 'Courier New', Courier, monospace; font-size: var(--fs); 
    line-height: var(--lh); white-space: pre-wrap; word-wrap: break-word; 
    overflow-y: auto; margin: 0; tab-size: 4;
}

.text-red { color: #d32f2f; font-weight: 500; }
.text-black { color: #222; font-weight: 500; }
.text-blue { color: #4a86e8; font-style: italic; font-weight: 500; }
.text-subtotal { color: #000; font-weight: 900; }
.fw-bold { font-weight: 900 !important; }

.item-count { color: #888; font-size: 0.7em; font-weight: normal; margin-left: 8px; font-family: sans-serif; }

.op-wrapper { position: relative; display: inline-block; }

.line-number {
    position: absolute; right: 46px; top: 0; color: #7b8db0; 
    font-size: 0.7em; font-weight: bold; font-family: sans-serif;
    user-select: none; pointer-events: none;
}

.copy-sub {
    position: absolute; right: 26px; top: 2px; color: #666; cursor: pointer; 
    transition: all 0.2s; display: inline-flex; align-items: center; justify-content: center;
}
.copy-sub:hover { color: #4a86e8; transform: scale(1.15); }

.keyboard-wrapper {
    background: #ffffff; border-radius: 12px; padding: 12px;
    box-shadow: 0 -4px 15px rgba(0,0,0,0.2); transition: all 0.3s ease; flex-shrink: 0; overflow: hidden;
}
.keyboard-wrapper.hidden { max-height: 0; padding: 0 12px; margin-bottom: 0; opacity: 0; }

.keyboard { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; height: 100%; }

.keyboard button {
    border: none; border-radius: 8px; font-size: 18px; font-weight: bold; cursor: pointer;
    background: #f0f2f5; color: #333; padding: 18px 0; font-family: sans-serif; -webkit-tap-highlight-color: transparent;
}
.keyboard button:active { transform: scale(0.95); filter: brightness(0.9); }
.btn-ac { color: #d32f2f !important; }
.btn-back { background: #6b7a90 !important; color: white !important; }
.btn-op { color: #e78b30 !important; }
.btn-minus { color: #d8515f !important; background: #ffebee !important; }
.btn-plus { color: #4caf50 !important; background: #e8f5e9 !important; }
.btn-enter { background: #5155d3 !important; color: white !important; }
.btn-action { background: #d0e0ff !important; color: #003c8f !important; }

.modal-overlay {
    display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.6); z-index: 1000; justify-content: center; align-items: center;
}
.modal {
    background: #fff; padding: 25px; border-radius: 12px; width: 90%; max-width: 380px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3); font-family: sans-serif;
}
.modal h2 { margin-top: 0; font-size: 20px; color: #333; border-bottom: 1px solid #eee; padding-bottom: 10px; }
.modal-row { margin: 15px 0; display: flex; justify-content: space-between; align-items: center; color: #555; font-size: 14px;}
.modal-row input[type="number"] { width: 60px; padding: 5px; border: 1px solid #ccc; border-radius: 4px; text-align: center; }
.modal-row input[type="checkbox"] { width: 20px; height: 20px; }
.radio-group { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.radio-group label { display: flex; align-items: center; gap: 6px; cursor: pointer; color: #333; font-weight: bold;}
.radio-desc { font-size: 12px; color: #888; margin-left: 24px; margin-top: -4px;}
.modal-btn { background: #4a86e8; color: white; border: none; padding: 12px; border-radius: 6px; width: 100%; font-size: 16px; font-weight: bold; cursor: pointer; margin-top: 15px;}

.toast {
    position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
    background: rgba(0,0,0,0.8); color: white; padding: 10px 20px; border-radius: 20px;
    font-size: 14px; opacity: 0; transition: opacity 0.3s; z-index: 9999; pointer-events: none;
}
.toast.show { opacity: 1; }

.print-footer { display: none; }

@media (max-width: 600px) {
    .app-container { padding: 5px; }
    .header h3 { font-size: 18px; }
    .toggle-btn { padding: 6px 10px; font-size: 13px; }
    .toggle-btn span { display: none; } 
    .toggle-btn.icon-only span { display: inline; } 
    
    .paper-container::before { left: 35px; }
    .backdrop { padding: 4px 5px 4px 43px; }
    .editor { left: 35px; width: calc(100% - 35px); padding-left: 8px; }
    
    .line-number { right: 38px; }
    .copy-sub { right: 20px; }
    
    .keyboard-wrapper { padding: 8px; }
    .keyboard { gap: 5px; }
    .keyboard button { padding: 16px 0; font-size: 18px; }
}

@media print {
    @page { size: A4; margin: 1.5cm; }
    body, html { margin: 0 !important; padding: 0 !important; width: 100% !important; background: #fff !important; }
    .app-container { max-width: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important; }
    .header, .keyboard-wrapper, .modal-overlay, .tabs-container, .copy-sub { display: none !important; }
    
    .paper-container { 
        box-shadow: none !important; border: 1px solid #ccc !important; 
        min-height: auto !important; height: auto !important; overflow: visible !important; 
        width: 100% !important; background: #fff !important; margin: 0 !important;
    }
    .editor { display: none !important; }
    .backdrop { 
        position: static !important; overflow: visible !important; height: auto !important; 
        padding: 10px 10px 10px 45px !important; white-space: pre-wrap !important; 
        word-wrap: break-word !important; font-size: 12pt !important; line-height: 1.5 !important;
        color: #000 !important;
    }
    .paper-container::before { left: 35px !important; }
    .line-number { right: auto !important; left: 10px !important; color: #555 !important; }
    
    .print-footer { 
        display: block !important; position: fixed; bottom: 0; width: 100%; 
        text-align: center; font-size: 10px; color: #333; font-family: sans-serif;
        border-top: 1px solid #ddd; padding-top: 5px;
    }
}